February 2019
Beginner
694 pages
18h 4m
English
The TypeScript syntax for declaring the type of a variable is to include a colon (:) after the variable name, and then indicate its type. As an example of this, let's rewrite our problematic doCalculation function to only accept numbers. Consider the following TypeScript code:
function doCalculation(
a : number,
b : number,
c : number) {
return ( a * b ) + c;
}
var result = doCalculation(3,2,1);
console.log("doCalculation():" + result);
Here, we have specified that the doCalculation function needs to be invoked with three numbers. This means that the properties a, b, and c must be of type number in order to call this function. If we now attempt to call this function with strings, as we did with the JavaScript sample, as follows: ...
Read now
Unlock full access