Roadmap › TypeScript — Types, Generics and Real-World Patterns › Module: TypeScript Basics
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| Type | Meaning |
|---|---|
any | Opt out of checking — errors spread silently |
unknown | Anything, but you must check the type before using it |
never | No value possible (functions that throw, exhaustive checks) |
void | Function returns nothing useful |
null / undefined | Separate types under strict |
Interview tip: "Prefer
unknownoveranyfor data from outside (JSON, errors incatch) — it forces you to validate."