Nikhil Malik
Nikhil Malik

Reputation: 478

What is the deal with Type Script <>

This syntax is from the Angular2 Hero tutorial.

getHeroes(): Promise<Hero[]> == getHeroes(): (Promise: Hero[]) ? 

I am confused about the Promise<Hero[]> part, in particular. Is it a way to represent multiple types at a time? What does the area between the < and > represent?

I am new to TypeScript and Angular2, but I want to know.

Upvotes: 0

Views: 97

Answers (2)

Hasan Beheshti
Hasan Beheshti

Reputation: 3615

< SomeType > is a generic type in Typescript which means you can have a Class or Function with SubType of T and you can do anything to it without knowing its actual type in the function definition.

For example:

  DoSomething<T> (input : T) : T {
      // doing something on input 
      input += 2;
      // return something with <T> type
      return input;
  }

see the Typescript handbook on generics

Upvotes: 3

Ashley Grant
Ashley Grant

Reputation: 10887

It's how generics are specified. A Promise can return a value of some type. The stuff inside the <> specifies what this specific promise will return. You can get some more information here: https://www.typescriptlang.org/docs/handbook/generics.html

Upvotes: 1

Related Questions