Function
Function Types
//in functions, we can specify parameter variable to accept only certain types
let sayHi = (name:string) => {
return 'Hello ' + name;
}
let mysquare = (num:number) => {
return num*num;
}
Multiple parameters
const doSomething = (lastname:string, firstname:string, age:number) => {
return "hello " + lastname + firstname + ", you are " + age + " years old";
}
Default Value
const sayHi = (name:string='stranger') => {
return "hello! " + name;
}
Return type
const addNums = (x:number) :boolean => {
return Math.sign(x);;
}
Void type
//Should not return anything, if we return aything typescript will complain
const addNums = (x:number) :void => {
console.log(x);
}
Never type
//this function does not have a chance of returning anything
const gameLoop=():never => {
while(true){console.log('keep playing');}
}