Types in functions
Functions in TypeScript use types to define parameters and return values. This allows us to have a concrete signature of a function that needs to be followed by anyone who is calling the function. Since JavaScript, natively, does not support types, we can't do a similar thing in JavaScript. If you take a look at the preceding example, where we had a function called printFullName, you can see that we have two parameters—firstName and lastName—whose type is defined as a string.
When we call this function, the type of value passed is checked by the compiler to confirm whether it matches the types defined in the function declaration. These types are also available inside the function body, which allows us to use features such ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access