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
Optional and default parameters
Example
Arrow functions and function types
Example
void: functions that return nothing
Example
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; }
