Typed function
Try it yourself →Functions with specified parameter and return types.
Syntax
function name(p: type): returnType { }Description
You assign types to every parameter and return value. The compiler warns you if you provide the wrong type.
Example
function square(x: number): number {
return x * x;
}
console.log(square(5));Expected output
25
When to use?
Safe, self-documenting functions.
Common mistakes
- ⚠️ square("5") → ERROR: argument must be a number.