Home > OS >  Typescript argument passing
Typescript argument passing

Time:11-28

While programming in Typescript, I got a question about the situation like below.
When I pass an argument with an object with the same name as the parameter defined in the function signature, Typescript understands it, otherwise it doesn't.

What is this feature of Typescript called? Is it also type of a type inference?

enter image description here

CodePudding user response:

TypeScript is simply checking the argument you are passing to func. You defined func as a function that takes a parameter which is an object with a key named bar.

The first and second calls of func work because the argument passed matches the condition, but the third one causes an error, since the object passed does not have bar property, but has bbar property.

  • Related