Tutorial by Examples

Optional Parameters In TypeScript, every parameter is assumed to be required by the function. You can add a ? at the end of a parameter name to set it as optional. For example, the lastName parameter of this function is optional: function buildName(firstName: string, lastName?: string) { // ...
Named functions function multiply(a, b) { return a * b; } Anonymous functions let multiply = function(a, b) { return a * b; }; Lambda / arrow functions let multiply = (a, b) => { return a * b; };
Suppose we want to receive a function as a parameter, we can do it like this: function foo(otherFunc: Function): void { ... } If we want to receive a constructor as a parameter: function foo(constructorFunc: { new() }) { new constructorFunc(); } function foo(constructorWithParams...
A TypeScript function can take in parameters of multiple, predefined types using union types. function whatTime(hour:number|string, minute:number|string):string{ return hour+':'+minute; } whatTime(1,30) //'1:30' whatTime('1',30) //'1:30' whatTime(1,'30') //'1:30' wha...

Page 1 of 1