Arrow Functions in TypeScript
In TypeScript, you can also make arrow functions. Let's look at an example. Let's say we have the following function:
let func = function(num: number): number {
return num ** 2;
}
Let's rewrite this function to the arrow version:
let func = (num: number): number => num ** 2;
Convert the following function to an arrow function:
let func = function(num1: number, num2: number): number {
return num1 + num2;
}
Convert the following function to an arrow function:
let func = function(str: string): string[] {
return str.split('');
}
Rewrite the following JavaScript code to TypeScript:
let arr = [1, 2, 3];
let res = arr.map(num => num ** 2);
console.log(res);