Interfaces and Type Aliases
- interface. An interface names an object shape once so every function can share it: interface User {id: string}.
- type aliases. A type alias names any type at all: objects, unions, functions: where interface handles objects only.
- Choosing between them. Teams pick one habit: interface for object shapes, type for unions and compositions: consistency beats doctrine.
Optional and Readonly Properties
- Optional properties. A question mark makes a property optional: nickname?: string means present or absent, and the checker enforces the check.
- readonly. The readonly modifier forbids reassignment after creation, documenting immutability the compiler defends.
- Honest optionality. Mark optional only what is truly sometimes absent: optional-everything types push undefined checks into every caller.
Structural Typing
- Shape over name. TypeScript types are structural: any value with the right properties fits, whatever its declared name.
- Excess property checks. Object literals get excess property checking: a typoed extra key fails right at the call site.
- Duck typing, checked. Structural typing is duck typing with proof: if it has the shape, it fits: and the compiler verified the quack.
Before and after this level
- Before: Level 1: TypeScript Foundations: Primitive Types and Inference, Variables, Functions and Objects, Reading TypeScript.
- Next: Level 3: Unions & Narrowing: Union and Intersection Types, Type Guards and Control-Flow Analysis, Discriminated Unions.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does TypeScript Level 2: Object Types cover?
Interfaces and Type Aliases, Optional and Readonly Properties, Structural Typing: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 1: TypeScript Foundations, which covers Primitive Types and Inference, Variables, Functions and Objects, Reading TypeScript.