Lessons in this course (9)

1. Basic types, inference and type annotations

Basic★ Must knowLesson 1 of 9
typescript
let name: string = "Divakar";
let age = 34;                    // inferred as number — annotate only when inference can't help
let active: boolean = true;
let ids: number[] = [1, 2, 3];   // or Array<number>
let pair: [string, number] = ["qty", 5];   // tuple

let anything: any = 4;           // turns type checking OFF — avoid
let input: unknown = JSON.parse("{}");     // safe "any": must narrow before use
function fail(msg: string): never { throw new Error(msg); }  // never returns
function log(msg: string): void { console.log(msg); }

let maybe: string | undefined;   // with strictNullChecks, undefined must be explicit
const status = "open" as const;  // literal type "open", not string
TypeMeaning
anyOpt out of checking — errors spread silently
unknownAnything, but you must check the type before using it
neverNo value possible (functions that throw, exhaustive checks)
voidFunction returns nothing useful
null / undefinedSeparate types under strict

Interview tip: "Prefer unknown over any for data from outside (JSON, errors in catch) — it forces you to validate."