🔷 TypeScript Interview Questions
Master TypeScript interviews with questions on types, interfaces, generics, and utility types
15-Minute TypeScript Cheatsheet
Quick reference for last-minute interview preparation
🎯 Basic Types
string, number, booleannull, undefined, voidany, unknown, neverarray: T[] or Array<T>tuple: [string, number]📋 Interface vs Type
interface extendstype = A | B (union)type = A & B (intersection)🔄 Generics
<T> - Type parameter<T extends U> - Constraint<T = Default> - Default typekeyof T - Keys of typeT[K] - Indexed access🛠️ Utility Types
Partial<T> - All optionalRequired<T> - All requiredPick<T, K> - Select keysOmit<T, K> - Exclude keysRecord<K, V> - Key-value map🛡️ Type Guards
typeof - Primitive checkinstanceof - Class checkin - Property checkis - Custom type guardas - Type assertion🚀 Advanced Types
Conditional: T extends U ? X : YMapped: [K in keyof T]infer - Extract typeas const - Literal typesatisfies - Type validation⚠️ Common Interview Gotchas
What is the difference between interface and type in TypeScript?
EasyBoth define object shapes, but interfaces support declaration merging and extension, while types support unions, intersections, and primitives.
Explain TypeScript generics with examples
MediumGenerics allow creating reusable components that work with multiple types while maintaining type safety.
What are utility types in TypeScript?
MediumUtility types are built-in TypeScript types that transform other types. They help avoid repetitive type definitions.
What is the difference between any, unknown, and never?
Mediumany bypasses type checking, unknown requires type checking before use, and never represents impossible values.
Explain type guards and type narrowing
MediumType guards are expressions that narrow a type within a conditional block. TypeScript uses control flow analysis to track types.
What are mapped types in TypeScript?
HardMapped types create new types by transforming each property of an existing type using a mapping syntax.
Explain conditional types in TypeScript
HardConditional types select one of two types based on a condition, using the syntax T extends U ? X : Y.
What are decorators in TypeScript?
HardDecorators are special declarations that modify classes, methods, properties, or parameters. They use the @expression syntax.
How do you handle null and undefined in TypeScript?
EasyTypeScript provides strict null checks and operators like optional chaining (?.) and nullish coalescing (??) to handle null/undefined safely.
What is declaration merging in TypeScript?
MediumDeclaration merging combines multiple declarations with the same name into a single definition. This works with interfaces, namespaces, and enums.
Explain enums in TypeScript
EasyEnums define a set of named constants. TypeScript supports numeric and string enums, with const enums for optimization.
What are template literal types?
HardTemplate literal types use template literal syntax to create new string literal types by combining and transforming other types.
How do you type React components in TypeScript?
MediumReact components can be typed using interfaces for props, generic types for flexibility, and specific types for hooks and events.
What is the infer keyword in TypeScript?
HardThe infer keyword extracts and infers types within conditional types, allowing you to capture parts of complex types.
What is structural typing in TypeScript?
MediumTypeScript uses structural typing (duck typing): types are compatible if they have the same structure, regardless of their declared names.
How do you configure TypeScript with tsconfig.json?
Mediumtsconfig.json configures the TypeScript compiler options, including strict type checking, module resolution, and output settings.
Key settings explained:
strict: Enables all strict type-checking optionstarget: ECMAScript version for outputmodule: Module system (ESNext, CommonJS)paths: Path aliases for cleaner imports
What are type assertions and when to use them?
EasyType assertions tell TypeScript to treat a value as a specific type. Use them when you know more about a type than TypeScript can infer.
Explain discriminated unions in TypeScript
MediumDiscriminated unions use a common literal property to distinguish between union members, enabling exhaustive type checking.
How do you create and use type-safe event emitters?
HardType-safe event emitters use TypeScript generics and mapped types to ensure event names and payloads are correctly typed.
What is module augmentation and global declarations?
HardModule augmentation extends existing modules with new declarations. Global declarations add types to the global scope.
Interview Tips for TypeScript
- ✓ Know the difference between interface and type aliases
- ✓ Understand generics and how to apply constraints
- ✓ Master utility types: Partial, Pick, Omit, Record
- ✓ Know type guards and narrowing techniques
- ✓ Understand structural vs nominal typing
- ✓ Be familiar with mapped and conditional types
- ✓ Know when to use any vs unknown vs never
- ✓ Practice typing React components and hooks