Functions
Implicit Declaration -> Signature of this function would be
- Input = Null
- Output = void
const print = () => {
console.log("Rishabh");
};
Return Type Defined -> Only boolean value has to be returned from this function. The number of input this function can take is 0.
const print = (): void => {
console.log("Hello");
};
const returnTrue = (): boolean => {
return true;
};
Input Parameters Defined -> This function can only have one argument and that can only be a number.
const alwaysEven = (num: number): number => {
return number * 2;
};
Prefer using this style
Different ways for defining function signature
- function
function alwaysEven(num: number) {
return num * 2;
}
function alwaysEven(num: number): number {
return num * 2;
}
- function as variable
const alwaysEven = function (num: number) {
return num * 2;
};
const alwaysEven = function (num: number): number {
return num * 2;
};
- arrow functions
const alwaysEven = (num: number): number => {
return num * 2;
};
const alwaysEven: (num: number) => number = (num) => {
return num * 2;
};
Higher Order Functions
const display = (printFunction: (message: string) => void): void => {
printFunction("Rishabh");
};
Functions signature can be defined using [[type]] or [[Interface]]