TypeScript FoundationsLesson 1.3
TypeScript primitive types: string, number, boolean, null, undefined
string type, number type, boolean type, null type, undefined type, type annotation syntax, implicit vs explicit types
The five primitive types
TypeScript's primitive types map directly to JavaScript primitives. You annotate them with a colon after the variable name:
let username: string = "alice";
let age: number = 30;
let isActive: boolean = true;
let middleName: string | null = null;
let score: number | undefined = undefined;Type inference
You rarely need to annotate primitives manually. TypeScript infers the type from the assigned value:
let city = "Berlin"; // inferred as string
city = 42; // Error: Type 'number' is not assignable to type 'string'Annotate explicitly when declaring a variable without an initial value:
let result: number;
result = fetchScore(); // assigned laternull and undefined
With strict mode enabled, null and undefined are not assignable to other types by default:
let name: string = null; // Error in strict mode
let name: string | null = null; // CorrectThis forces you to handle missing values explicitly, which eliminates an entire category of runtime errors. The | syntax is a union type — covered in depth later.
