AnyCompiler

Arrow Functions

Arrow functions in TypeScript work exactly like in JavaScript, just with the same type annotations available on their parameters and return value.

const square = (x: number): number => x * x;
console.log(square(5));
Output
25

Inferred return types

You don't have to annotate the return type explicitly — TypeScript infers it from the function body, and still enforces it at every call site exactly as if you had written it.

const numbers: number[] = [1, 2, 3, 4, 5];
const squared = numbers.map((x) => x * x);
console.log(squared);
Output
[ 1, 4, 9, 16, 25 ]

Next: enums — a named set of related constant values.

Practice exercise

Write an arrow function called cube that takes a number and returns its cube, then print cube(3).