TYPESCRIPT FUNCTIONS
TypeScript has a specific syntax for typing function parameters and return values.
- Return Type
function getTime(): number {
return new Date().getTime();
};
// with arrow function
const getFullYear = (): number => {
return new Date().getFullYear();
}
const time: number = getTime();
const year: number = getFullYear();
console.log(time);
console.log(year);
- Void Return Type
The type void can be used to indicate a function doesn't return any value.
function main (): void {
console.log('Hello world');
}
// arrow functions
const bye = (): void => {
console.log('Good bye! Crual World!');
}
main();
bye();
- Parameters
Function parameters are typed with a similar syntax as variable declarations.
function multiply (a: number, b: number): number {
return a * b;
}
// arrow function
const mult = (a: number, b: number): number => a *b;
const product: number = mult(148, 48);
const prod: number = multiply(5, 6);
console.log(product);
console.log(prod);
If no parameter type is defined, TypeScript will default to using any, unless additional type information is available as shown in the Default Parameters and Type Alias sections below.
- Optional Parameters
By default TypeScript will assume all parameters are required, but they can be explicitly marked as optional.
function add (a: number, b: number, c?:number): number {
return a + b + (c || 0);
}
// Arrow function
const addition = (a: number, b:number, c?:number): number => a + b + (c || 0);
const s: number = add(10, 38);
const sum: number = addition(12, 48, 39);
console.log(s);
console.log(sum);
- Default Parameters
For parameters with default values, the default value goes after the type annotation:
function pow (value: number, exponent: number = 10): number {
return value ** exponent;
}
// arrow function
const power = (value: number, exponent: number = 2): number => value ** exponent;
const resp: number = power(24, 3);
const result: number = pow(20);
console.log(result);
console.log(resp);
TypeScript can also infer the type from the default value.
- Named Parameters
Typing named parameters follows the same pattern as typing normal parameters.
function divise({ dividend, divisor}: {dividend: number, divisor: number}): number{
return dividend / divisor;
};
console.log(divise({ dividend: 20, divisor: 4}));
// or we can do this - long way
type ParameterType = {
dividend: number;
divisor: number;
};
const diviseN = ({ dividend, divisor }: ParameterType): number => dividend / divisor;
const args: ParameterType = {
dividend: 204,
divisor: 24,
};
console.log(diviseN(args));
- Rest Parameters
Rest parameters can be typed like normal parameters, but the type must be an array as rest parameters are always arrays.
function add(a: number, b: number, ...rest: number[]) {
return a + b + rest.reduce((p:number, c:number) => p + c, 0);
}
// arrow function
const addition = (a:number, b: number, ...rest: number[]): number => {
return a + b + rest.reduce((ac: number, cv:number) => ac + cv, 0);
}
const arr = [1, 2, 3];
console.log(add(5, 3, ...arr));
console.log(addition(2, 3, ...arr));
- Type Alias
Function types can be specified separately from functions with type aliases.
type Negate = (value: number) => number;
// in this function, the parameter `value` automatically gets assigned the type `number` from the type `Negate`
const negateFunction: Negate = (value) => -Math.abs(value);
console.log(negateFunction(48));
Top comments (0)