Inferring the types

Inferring the types

In playing with the example, you may notice that the TypeScript compiler can figure out the type if you have types on one side of the equation but not the other:

// myAdd has the full function type
let myAdd = function(x: number, y: number): number { return  x + y; };

// The parameters 'x' and 'y' have the type number
let myAdd: (baseValue:number, increment:number) => number =
  function(x, y) { return x + y; };

This is called “contextual typing”, a form of type inference. This helps cut down on the amount of effort to keep your program typed.

doc_TypeScript
2016-10-04 19:25:17
Comments
Leave a Comment

Please login to continue.