February 2019
Beginner
694 pages
18h 4m
English
Using the very simple syntactic sugar that TypeScript uses, we can easily define the type of a variable that a function should return. In other words, when we call a function, and it returns a value, what type should the return value be treated as?
Consider the following TypeScript code:
function addNumbers(a: number, b: number) : string {
return a + b;
}
var addResult = addNumbers(2,3);
console.log(`addNumbers returned : ${addResult}`);
Here, we have added a :number type to both of the parameters of the addNumbers function (a and b), and we have also added a :string type just after the ( ) braces in the function definition. Placing a type annotation after the function definition means that we are defining the return ...
Read now
Unlock full access