skube
skube

Reputation: 6175

Redundant typing in writing simple function type

Looking at the docs regarding Typescript, they show an example of writing the function type.

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

It took me a while to decipher the above and when I finally did, it seemed like redundant typing.

Doesn't myAdd automatically get the typing defined from the right side of the expression? I don't see the point of basically defining it again (in a slightly different format) on the left.

What am I still not understanding?

Upvotes: 0

Views: 141

Answers (1)

Jeff Bowman
Jeff Bowman

Reputation: 95754

Are you talking about the section immediately following, "Inferring the types"?

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

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

Upvotes: 1

Related Questions