PrepPilotPractise free

TypeScript interview questions

Types, generics, narrowing, utility types, config

17 questions · tsc · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is structural typing in TypeScript?

Structural typing means that TypeScript checks type compatibility based on the actual properties and methods present, not on the explicit type or interface name. If two objects have the same shape, they are considered compatible.

When would you use an interface instead of a type alias?

I use an interface when describing the shape of an object, especially if I expect to extend it later or allow declaration merging. Interfaces are best for defining contracts for classes or objects.

What does the unknown type represent, and how is it different from any?

unknown is a type-safe counterpart to any. You can assign any value to unknown, but you must perform type checks before using it, which helps prevent runtime errors. With any, you lose all type safety.

What does the never type mean in TypeScript?

never represents values that never occur, such as the return type of a function that always throws an error or never finishes executing. It's useful for exhaustive checks in switch statements.

Intermediate questions

How do you narrow a union type in a function?

I use type guards, such as checking a discriminant property or using typeof or instanceof, to narrow the type. For example, with a union { kind: 'a' } | { kind: 'b' }, I check if (obj.kind === 'a') to safely access properties specific to that variant.

What is a discriminated union and why is it useful?

A discriminated union is a union of object types that share a common literal property, usually called kind or type. It allows TypeScript to narrow the type based on the value of that property, making code safer and more readable.

Explain the difference between Partial<T> and Omit<T, K>.

Partial<T> makes all properties of T optional, which is useful for update patterns. Omit<T, K> creates a type by removing the specified keys K from T, which is helpful when you want to exclude certain properties.

How do you constrain a generic type parameter?

I use the extends keyword to constrain the generic parameter. For example, function foo<T extends { id: number }>(arg: T) ensures that arg must have an id property of type number.

What is the purpose of the ReturnType<T> utility type?

ReturnType<T> extracts the return type of a function type T. It's useful when you want to reuse or reference the output type of a function elsewhere in your codebase.

How does TypeScript handle type compatibility between interfaces and type aliases with the same structure?

TypeScript treats them as compatible if their structures match, due to structural typing. It doesn't matter if one is an interface and the other is a type alias; only the shape matters.

What are some important options to set in tsconfig.json for a new project?

I always enable strict for full type safety, set target to the desired ECMAScript version, use module as needed (like commonjs or esnext), enable esModuleInterop for compatibility, and specify outDir for build output.

How does Record<K, T> work and when would you use it?

Record<K, T> creates an object type with keys of type K and values of type T. I use it when I need a dictionary-like object, such as mapping strings to user objects: Record<string, User>.

Senior questions

How would you type and validate an API request payload in a Node.js app?

See the answer in the app, free

Describe a scenario where using any caused a production bug, and how you would prevent it.

See the answer in the app, free

How do you ensure exhaustive checks in a switch statement over a discriminated union?

See the answer in the app, free

What are the trade-offs between using interfaces and type aliases in a large codebase?

See the answer in the app, free

How do you handle third-party data (e.g., from external APIs) safely in TypeScript?

See the answer in the app, free

More JavaScript & Node.js topics