Core Type System & Inference
📖 Concept
TypeScript's type system is structural (also called duck typing) — it cares about the shape of data, not its name or origin. If an object has the right properties, it's compatible, regardless of where it came from.
Primitive types: string, number, boolean, null, undefined, bigint, symbol
Special types: any (escape hatch), unknown (safe any), never (impossible), void (no return)
Object types: object, {}, arrays, tuples, functions
Type Inference is TypeScript's ability to automatically determine types without explicit annotations. TS uses control flow analysis to narrow types through your code.
Structural Typing vs Nominal Typing:
- Structural (TypeScript): Two types are compatible if they have the same shape/structure
- Nominal (Java/C#): Two types are compatible only if they have the same name/declaration
This means in TypeScript, you never need implements for a class to satisfy an interface — if it has the right shape, it works.
Type vs Value space: TypeScript has two parallel worlds — the type space (erased at runtime) and the value space (exists at runtime). interface and type live only in type space; class lives in both.
🏠 Real-world analogy: Structural typing is like a USB port. It doesn't care what brand the cable is — if the plug fits the shape (has the right pins), it works. Nominal typing would require the cable to be from the same manufacturer.
💻 Code Example
1// Primitive types2let name: string = "Alice";3let age: number = 30;4let active: boolean = true;5let big: bigint = 9007199254740991n;6let id: symbol = Symbol("id");78// Special types9let anything: any = 42; // ⚠️ Escape hatch — disables type checking10anything = "now a string"; // No error — defeats the purpose of TS!1112let safe: unknown = 42; // ✅ Safe alternative to any13// safe.toFixed(); // ❌ Error! Must narrow first14if (typeof safe === "number") {15 safe.toFixed(2); // ✅ After narrowing, it's safe16}1718function throwError(msg: string): never {19 throw new Error(msg); // never returns — the return type is 'never'20}2122function log(msg: string): void {23 console.log(msg); // void = no meaningful return value24}2526// Type inference in action27let x = 10; // inferred as 'number'28let y = [1, 2, 3]; // inferred as 'number[]'29let z = { name: "Alice" }; // inferred as '{ name: string }'3031// const narrows to literal types32const pi = 3.14; // type: 3.14 (literal), not 'number'33const greeting = "hello"; // type: "hello" (literal), not 'string'3435// Structural typing — shape matters, not name36interface Point {37 x: number;38 y: number;39}40function distance(p: Point): number {41 return Math.sqrt(p.x ** 2 + p.y ** 2);42}43// No 'implements Point' needed — just match the shape!44const myPoint = { x: 3, y: 4, z: 5 }; // Extra property OK45distance(myPoint); // ✅ Works! Has x and y4647// Arrays and tuples48let nums: number[] = [1, 2, 3];49let pair: [string, number] = ["age", 30]; // Fixed length & types50let readonly_arr: readonly number[] = [1, 2, 3];51// readonly_arr.push(4); // ❌ Error: Property 'push' does not exist
🏋️ Practice Exercise
Mini Exercise:
- Declare variables using each primitive type and each special type
- Show the difference between
anyandunknownby trying to call methods on both - Create an object that satisfies an interface WITHOUT explicitly implementing it (structural typing)
- Use
constvsletdeclarations and observe how TypeScript infers literal types vs wider types - Create a tuple type for a coordinate
[number, number, number]and try adding a 4th element
⚠️ Common Mistakes
Using
anyinstead ofunknown—anydisables ALL type checking;unknownforces you to narrow before useConfusing
voidwithundefined—voidmeans 'no meaningful return'; avoidfunction CAN returnundefinedimplicitlyNot understanding structural typing — coming from Java/C#, developers expect nominal typing and add unnecessary
implementsclausesThinking
neveris the same asvoid—voidreturns nothing;nevermeans the function NEVER returns (throws or infinite loop)Over-annotating when inference would suffice —
let x: number = 5is redundant;let x = 5is cleaner and equally type-safe
💼 Interview Questions
🎤 Mock Interview
Practice a live interview for Core Type System & Inference