2.8 Type Aliases vs Interfaces (Week 2 • TypeScript 5)
1. Core Concept & Internal Working
Type Aliases vs Interfaces (TypeScript 5) covers Structural compatibility, `interface` `extends` & declaration merging (needed for augmenting `Express.Request.user`!) vs `type` aliases supporting Unions, Tuples, and Mapped Types. In production software engineering, it is used to Using `declare global { namespace Express { interface Request { user?: AuthPayload } } }` to type `req.user` cleanly across middleware! Mastering its underlying execution rules and edge-case boundaries lets you write reliable, maintainable code.
2. Real-World Engineering Use Case
Using `declare global { namespace Express { interface Request { user?: AuthPayload } } }` to type `req.user` cleanly across middleware!
3. Granular Subtopics Covered
- Structural compatibility: Core foundation of Type Aliases vs Interfaces: Using `declare global { namespace Express { interface Request { user?: AuthPayload } } }` to type `req.user` cleanly across middleware!
- `interface` `extends` & declaration merging (needed for augmenting `Express.Request.user`!) vs `type` aliases supporting Unions: Master '`interface` `extends` & declaration merging (needed for augmenting `Express.Request.user`!) vs `type` aliases supporting Unions' inside Type Aliases vs Interfaces across real-world inputs.
- Tuples: Master 'Tuples' inside Type Aliases vs Interfaces across real-world inputs.
- Mapped Types: Master 'Mapped Types' inside Type Aliases vs Interfaces across real-world inputs.
- Production Pitfall Guard (Type Aliases vs Interfaces): Prevent common defect: Casting `(req as any).user` inside 30 controllers instead of augmenting `Express.Request` once in `src/types/express.d.ts`!
- Hands-On Verification (Type Aliases vs Interfaces): Augment `Express.Request` with `requestId: string` and `user?: AuthenticatedUser` cleanly.
4. Reference Implementation
export interface AuthenticatedUser { readonly sub: string; readonly role: 'ADMIN' | 'USER'; }
// Augment Express.Request once in types/express.d.ts — zero (req as any) needed!
5. Common Mistakes to Avoid
- Casting `(req as any).user` inside 30 controllers instead of augmenting `Express.Request` once in `src/types/express.d.ts`!
- Skipping boundary, null/empty, or error-state verification when implementing Type Aliases vs Interfaces.
- Pushing untested Type Aliases vs Interfaces changes without clean naming and Git commit context.
6. Hands-On Exercise
Augment `Express.Request` with `requestId: string` and `user?: AuthenticatedUser` cleanly.
7. Technical Interview Checkpoints
- Q: What problem does Type Aliases vs Interfaces solve in TypeScript 5, and what fails if it is misused?
Key Focus: Using `declare global { namespace Express { interface Request { user?: AuthPayload } } }` to type `req.user` cleanly across middleware! Guard against: Casting `(req as any).user` inside 30 controllers instead of augmenting `Express.Request` once in `src/types/express.d.ts`! - Q: Explain the internal working of Type Aliases vs Interfaces (Structural compatibility, `interface` `extends` & declaration merging (needed for augmenting `Express.Request.user`!) vs `type` aliases supporting Unions, Tuples, and Mapped Types).
Key Focus: Walk step-by-step through Structural compatibility, `interface` `extends` & declaration merging (needed for augmenting `Express.Request.user`!) vs `type` aliases supporting Unions, Tuples, and Mapped Types with a concrete code example. - Q: How did you apply and verify Type Aliases vs Interfaces in your hands-on lab?
Key Focus: Augment `Express.Request` with `requestId: string` and `user?: AuthenticatedUser` cleanly.