DAY 1 OF 30
Primitive types
Yesterday, in one sentence
It checks your types and reports any type errors, then removes all the types and outputs plain JavaScript.
Did you say:
- ✓checks types
- ✓reports type errors
- ✓removes types
- ✓outputs plain JavaScript
Interview question
Statically typed. The compiler checks the types at build time, before the code runs.
Did you say:
- ✓statically typed
- ✓checked at build time
- ✓before the code runs
A primitive type is one of the seven basic types built into TypeScript: string,
number, boolean, null, undefined, symbol, bigint. You attach one to a
variable with a colon after its name. This is called a type annotation.
The TypeScript compiler checks these annotations. If you assign a value that does not match the annotated type and you get an error.
let title: string = "Day 1";
let done: boolean = false;
let count: number = 30;
count = 31; // ok
count = "thirty"; // ❌ Type 'string' is not assignable to type 'number'
At build timeBuild Time the compiler turns all TypeScript into JavaScript by stripping out every type annotation, so the JavaScript that ships has no types in it at all.
Check yourself
null, boolean and symbol are all primitives. object is not, and null being a primitive is the one people forget.
string, number, boolean, null, undefined, symbol, bigint.
The same ones JavaScript has.
Did you say:
- ✓string, number, boolean
- ✓null, undefined
- ✓symbol, bigint