← Learning Hub
Beginner

Typed Functions

Give each parameter a type, and optionally the return type. Then TypeScript checks every call.

Functions are where types help most: the parameter types say exactly what callers must pass, and the return type says what they'll get back. Your editor then shows these hints everywhere the function is used.

Example
You can edit this code.

Optional and default parameters

Example
You can edit this code.

Arrow functions and function types

Example
You can edit this code.

void: functions that return nothing

Example
You can edit this code.
Key points
  • Type parameters and return values.
  • ? makes parameters optional; = gives defaults.
  • void means no return value.

📝 Test yourself

1. What does city?: string mean in a parameter list?
2. What return type do you use when a function returns nothing?
3. Fill in the return type:
function total(a: number, b: number): { return a + b; }