DAY 2 OF 30
Type inference
Yesterday, in one sentence
string, number, boolean, null, undefined, symbol, bigint.
The same ones JavaScript has.
Did you say:
- ✓string, number, boolean
- ✓null, undefined
- ✓symbol, bigint
Interview question
A type annotation is you writing the type explicitly, with a colon after the variable name. The compiler checks them, then strips them all out, so the JavaScript that ships has no types in it.
Did you say:
- ✓colon after the name
- ✓compiler checks them
- ✓stripped at build time
Type inference is TypeScript working out a type from the value you assign, without you writing a type annotationType Annotation.
let title = "Day 2"; // inferred as string
let count = 30; // inferred as number
count = 31; // ok
count = "thirty"; // ❌ Type 'string' is not assignable to type 'number'
Inference is the default, and most TypeScript you write has no annotations in it at all. The value tells the compiler everything it needs.
Check yourself
count here?
let count = 30
TypeScript uses the value to pick the type. Because count can be reassigned, it picks the general number rather than the exact value 30.
TypeScript working out the type from the assigned value, instead of you annotating it.
Did you say:
- ✓TypeScript works out the type from the value
- ✓no annotation written