get and set in TypeScript

TypeScript uses getter/setter syntax that is like ECMAScript4/ActionScript3. class foo { private _bar: boolean = false; get bar(): boolean { return this._bar; } set bar(value: boolean) { this._bar = value; } } That will produce this JavaScript, using the ECMAScript 5 Object.defineProperty() feature. var foo = (function () { function foo() { this._bar = false; … Read more

In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?

That’s the non-null assertion operator. It is a way to tell the compiler “this expression cannot be null or undefined here, so don’t complain about the possibility of it being null or undefined.” Sometimes the type checker is unable to make that determination itself. It is explained here: A new ! post-fix expression operator may … Read more

Can’t bind to ‘formGroup’ since it isn’t a known property of ‘form’

RC6/RC7/Final release FIX To fix this error, you just need to import ReactiveFormsModule from @angular/forms in your module. Here’s the example of a basic module with ReactiveFormsModule import: import { NgModule } from ‘@angular/core’; import { BrowserModule } from ‘@angular/platform-browser’; import { FormsModule, ReactiveFormsModule } from ‘@angular/forms’; import { AppComponent } from ‘./app.component’; @NgModule({ imports: … Read more

Difference between Constructor and ngOnInit

The Constructor is a default method of the class that is executed when the class is instantiated and ensures proper initialisation of fields in the class and its subclasses. Angular, or better Dependency Injector (DI), analyses the constructor parameters and when it creates a new instance by calling new MyClass() it tries to find providers … Read more

What is TypeScript and why would I use it in place of JavaScript? [closed]

I originally wrote this answer when TypeScript was still hot-off-the-presses. Five years later, this is an OK overview, but look at Lodewijk’s answer below for more depth 1000ft view… TypeScript is a superset of JavaScript which primarily provides optional static typing, classes and interfaces. One of the big benefits is to enable IDEs to provide … Read more