Type annotations
A type annotation is a colon followed by a type: const width: number = 8;
tells TypeScript (and anyone reading the code) that width will always hold a
number. Functions work the same way - function area(w: number, h: number): number types each parameter and, after the closing ), the value the
function returns.
Annotations don't change what runs - they're erased before your code executes
(that's exactly what this playground's TypeScript step does: strip the types,
run the plain JavaScript underneath). Their job is to catch mistakes in your
editor, and to document what a function expects and hands back.
Your task: finish area(w, h) so it returns w * h, with w, h, and
the return value all typed as number.
You'll practice:
Typing a variable with : number
Typing function parameters and a return value
Show a hint
Show solution
Related reading: Why Types & the Basic Types - What the Checker Buys You →
Next