TypeScript Advanced Interview Questions
Difference between interface and type alias? Interface: extendable via extends/implements, declaration merging. Type: can express unions, intersections, mapped types, template literals. Prefer interface for objects/classes; type for complex types
What is structural typing? TS uses duck typing — if an object has all required properties, it satisfies the type regardless of its class/origin. Contrast with nominal typing (Java/C#) where types must explicitly declare conformance
What is the infer keyword? Used in conditional types to capture a type variable: T extends Promise<infer R> ? R : never extracts the resolved type from a Promise
Covariance vs contravariance? Function return types are covariant (can return subtype). Function param types are contravariant (param must accept supertype). TypeScript function params are bivariant by default (use --strictFunctionTypes)
When to use unknown vs any? unknown is the type-safe any — you must narrow it before use. Use unknown for values you don't control (API responses, error catches). Avoid any; it disables type checking
What is a discriminated union? Union of types sharing a literal discriminant field (e.g., type: "circle" | "square"). TypeScript narrows the type based on the discriminant in switch/if statements
How to make a deep readonly type? Mapped type: type DeepReadonly<T> = { readonly [K in keyof T]: T[K] extends object ? DeepReadonly<T[K]> : T[K] }
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free