Typed inputs and output
A function is a small machine. Values go in, it does its work, and one value comes out. Typing a function means writing down which kinds of values may go in, and which kind comes out.
The values a function takes are its parameters, here w and h. Writing w: number gives each one a type, like a slot shaped for numbers only. Without a type, TypeScript cannot check what goes in.
tsc checks every call
Line 4 calls area(3, 4). The values in the call are arguments. tsc matches them to the slots in order: 3 goes to w, 4 goes to h. Both are numbers, so the call passes.
The : number after the brackets is the return type: the kind of value that comes out. tsc checks line 2: w * h is a number, so it matches. The answer, 12, goes back to size.
Say line 2 becomes return w * h + " cm". Adding text makes a string, not a number. tsc reports on line 2: Type 'string' is not assignable to type 'number'. The return type caught a bug inside the function.
Line 2 is back to w * h. Now call area(3) with one value. Slot h stays empty, so tsc says: Expected 2 arguments, but got 1. Plain JavaScript would run it and give NaN, meaning not a number.
Default values
Give h a default value: h = w. If a call leaves h out, it takes the value of w, so area(3) is a 3 by 3 square. TypeScript infers h is a number from the default, and the call passes.
With no errors, tsc writes plain JavaScript and Node.js runs it. area(3) gives 3 * 3 = 9, and console.log prints 9. TypeScript also knows size is a number, from the return type.
In your editor
Editors read the types and show them as you type area(: area(w: number, h?: number): number. The ? on h means you may leave it out. One line tells every caller what goes in and what comes out.
What happens
;; step 1/9 · machine
area.ts
> 1 function area(w: number, h: number): number {
> 2 return w * h;
> 3 }
4 const size = area(3, 4);
5 console.log(size);
area takes a width w and a height h
and gives back w * h
You can often leave the return type out: TypeScript infers it from the return lines. Writing it anyway makes the promise explicit, so a wrong return is reported inside the function instead of where the result is used. tsc 5.9 with no settings writes older JavaScript: it turns const into var and h = w into an if statement. Settings such as target change those details; the types are always removed.