Typing the function

Typing the function

Let’s add types to our simple examples from earlier:

function add(x: number, y: number): number {
  return x + y;
}

let myAdd = function(x: number, y: number): number { return x+y; };

We can add types to each of the parameters and then to the function itself to add a return type. TypeScript can figure the return type out by looking at the return statements, so we can also optionally leave this off in many cases.

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

Please login to continue.