Typing the function

Typing the function

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

1
2
3
4
5
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
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.