shrimpdrake shrimpdrake - 3 months ago 15
TypeScript Question

<> syntax in Typescript Angular2

I'm new to Angular2 and I don't understand that <> syntax :

example 1 ( from https://angular.io/docs/ts/latest/guide/dependency-injection.html ) :

let mockService = <HeroService> {getHeroes: () => expectedHeroes }


example 2 ( from https://angular.io/docs/ts/latest/guide/template-syntax.html )

deleteRequest = new EventEmitter<Hero>();


Any help or reference to a comprehensive answer is appreciated.

Answer

<HeroService> {getHeroes: () => expectedHeroes } corresponds to a cast / type assertion to the HeroService class. The returned object will follow the structure of this class.

new EventEmitter<Hero>(); is a way to parametrize the EventEmitter class (generics) to tell that it'll handle elements of type Hero. In the case of the EventEmitter class (see https://github.com/angular/angular/blob/master/modules/%40angular/facade/src/async.ts#L80), it ensures that objects used as a parameter of the emit method must be of type Hero.

See the following links for more details: