🔷 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, boolean
null, undefined, void
any, unknown, never
array: T[] or Array<T>
tuple: [string, number]

📋 Interface vs Type

Interface: Extendable, declaration merging
Type: Unions, intersections, primitives
interface extends
type = A | B (union)
type = A & B (intersection)

🔄 Generics

<T> - Type parameter
<T extends U> - Constraint
<T = Default> - Default type
keyof T - Keys of type
T[K] - Indexed access

🛠️ Utility Types

Partial<T> - All optional
Required<T> - All required
Pick<T, K> - Select keys
Omit<T, K> - Exclude keys
Record<K, V> - Key-value map

🛡️ Type Guards

typeof - Primitive check
instanceof - Class check
in - Property check
is - Custom type guard
as - Type assertion

🚀 Advanced Types

Conditional: T extends U ? X : Y
Mapped: [K in keyof T]
infer - Extract type
as const - Literal type
satisfies - Type validation

⚠️ Common Interview Gotchas

• any disables type checking (avoid it)
• unknown is safer than any
• Interface can be merged, type cannot
• readonly vs const (properties vs variables)
• Structural typing (duck typing)
• Type narrowing with control flow

What is the difference between interface and type in TypeScript?

Easy

Both define object shapes, but interfaces support declaration merging and extension, while types support unions, intersections, and primitives.

typescript

Explain TypeScript generics with examples

Medium

Generics allow creating reusable components that work with multiple types while maintaining type safety.

typescript

What are utility types in TypeScript?

Medium

Utility types are built-in TypeScript types that transform other types. They help avoid repetitive type definitions.

typescript

What is the difference between any, unknown, and never?

Medium

any bypasses type checking, unknown requires type checking before use, and never represents impossible values.

typescript

Explain type guards and type narrowing

Medium

Type guards are expressions that narrow a type within a conditional block. TypeScript uses control flow analysis to track types.

typescript

What are mapped types in TypeScript?

Hard

Mapped types create new types by transforming each property of an existing type using a mapping syntax.

typescript

Explain conditional types in TypeScript

Hard

Conditional types select one of two types based on a condition, using the syntax T extends U ? X : Y.

typescript

What are decorators in TypeScript?

Hard

Decorators are special declarations that modify classes, methods, properties, or parameters. They use the @expression syntax.

typescript

How do you handle null and undefined in TypeScript?

Easy

TypeScript provides strict null checks and operators like optional chaining (?.) and nullish coalescing (??) to handle null/undefined safely.

typescript

What is declaration merging in TypeScript?

Medium

Declaration merging combines multiple declarations with the same name into a single definition. This works with interfaces, namespaces, and enums.

typescript

Explain enums in TypeScript

Easy

Enums define a set of named constants. TypeScript supports numeric and string enums, with const enums for optimization.

typescript

What are template literal types?

Hard

Template literal types use template literal syntax to create new string literal types by combining and transforming other types.

typescript

How do you type React components in TypeScript?

Medium

React components can be typed using interfaces for props, generic types for flexibility, and specific types for hooks and events.

typescript

What is the infer keyword in TypeScript?

Hard

The infer keyword extracts and infers types within conditional types, allowing you to capture parts of complex types.

typescript

What is structural typing in TypeScript?

Medium

TypeScript uses structural typing (duck typing): types are compatible if they have the same structure, regardless of their declared names.

typescript

How do you configure TypeScript with tsconfig.json?

Medium

tsconfig.json configures the TypeScript compiler options, including strict type checking, module resolution, and output settings.

json

Key settings explained:

  • strict: Enables all strict type-checking options
  • target: ECMAScript version for output
  • module: Module system (ESNext, CommonJS)
  • paths: Path aliases for cleaner imports

What are type assertions and when to use them?

Easy

Type assertions tell TypeScript to treat a value as a specific type. Use them when you know more about a type than TypeScript can infer.

typescript

Explain discriminated unions in TypeScript

Medium

Discriminated unions use a common literal property to distinguish between union members, enabling exhaustive type checking.

typescript

How do you create and use type-safe event emitters?

Hard

Type-safe event emitters use TypeScript generics and mapped types to ensure event names and payloads are correctly typed.

typescript

What is module augmentation and global declarations?

Hard

Module augmentation extends existing modules with new declarations. Global declarations add types to the global scope.

typescript

Interview Tips for TypeScript