Tech

TypeScript

TypeScript adds static types on top of JavaScript to help teams catch bugs earlier, document intent more clearly, and scale codebases with safer refactoring. Interviews often focus on interfaces, type aliases, generics, unions and intersections, narrowing, utility types, and compiler strictness settings.

What you get

Questions

20

Difficulty

3 levels

Answer Formats

2

Use the toggle on each card to move between an interview-ready answer and a simpler explanation. Questions are sorted from beginner to advanced, and the keywords are highlighted. You can also blur the answers to practice recalling them from memory.

Questions

Practice the answers out loud.

All topics

Question 1

What is TypeScript and why would you use it?

Beginner

How to answer in an interview

TypeScript is a typed superset of JavaScript that compiles down to plain JavaScript. I would use it to catch mistakes earlier at compile time, make large codebases easier to navigate, and improve tooling such as autocompletion, refactoring, and documentation inside the editor. It is especially valuable on teams where multiple developers share and extend the same code over time.

Question 2

What is the difference between an interface and a type alias in TypeScript?

Intermediate

How to answer in an interview

Both interfaces and type aliases can describe object shapes, and in many cases they overlap. I usually prefer interface for object-oriented public contracts because it supports declaration merging and is easy to extend, while type aliases are more flexible for unions, intersections, primitive aliases, tuple types, and function signatures. The right choice depends on whether I need extensibility or more expressive composition.

Question 3

How do generics work in TypeScript?

Intermediate

How to answer in an interview

Generics let you write reusable functions, classes, and interfaces that preserve type information without forcing you to use 'any'. By introducing a type parameter such as T, you can keep the input and output types connected, which makes APIs safer and more flexible. Constraints with 'extends' let you require that a generic type has certain properties before it can be used.

Question 4

What is the difference between union and intersection types?

Intermediate

How to answer in an interview

A union type means a value can be one of several types, such as string | number, so you need to narrow before using it safely. An intersection type means a value must satisfy all of the combined types, such as combining shared behavior from multiple interfaces. I use unions when modeling alternatives and intersections when composing capabilities.

Question 5

What is type narrowing in TypeScript?

Intermediate

How to answer in an interview

Type narrowing is how TypeScript uses runtime checks and control flow to reduce a broad type to a more specific one. Common narrowing tools include 'typeof', 'instanceof', the 'in' operator, custom type guards, and discriminated unions. This is important because it lets you write safer code without losing the benefits of flexible type definitions.

Question 6

What are utility types in TypeScript?

Intermediate

How to answer in an interview

Utility types are built-in helper types that transform existing types into new ones, such as Partial, Pick, Omit, Readonly, Record, ReturnType, and Exclude. They reduce repetitive boilerplate and make type definitions easier to maintain because I can reuse and reshape existing models instead of redefining them from scratch.

Question 7

What is a discriminated union in TypeScript?

Intermediate

How to answer in an interview

A discriminated union is a union of types that share a common literal property (the discriminant), enabling TypeScript to narrow the correct type in a switch or if-else block. This pattern is extremely useful for modeling state machines, API responses, or any scenario where an object can be one of several shapes, and the discriminant tells you which shape it is.

Question 8

When would you use a type alias over an interface?

Intermediate

How to answer in an interview

I use type aliases when I need unions, intersections, tuple types, mapped types, or computed properties — things interfaces cannot express. I use interfaces when defining object shapes that might need declaration merging or where extends-based inheritance is cleaner. In practice, object-oriented public contracts usually use interfaces, while complex type-level compositions use type aliases.

Question 9

How do you type async functions and Promises in TypeScript?

Intermediate

How to answer in an interview

An async function always returns a Promise, so TypeScript automatically wraps the return type in Promise<T>. You can type a variable as Promise<T> or use the Awaited utility type to extract the resolved type. For callbacks returning Promises, you type the return as Promise<void> or the specific resolved type, and the compiler handles the rest.

Question 10

What are declaration files (.d.ts) and when are they used?

Intermediate

How to answer in an interview

Declaration files provide type information for JavaScript libraries that don't have built-in TypeScript types, allowing TypeScript to type-check code that uses those libraries. They're typically installed via @types packages from DefinitelyTyped or written manually for internal modules. A .d.ts file contains only type declarations, no runtime code.

Question 11

What are assertion functions in TypeScript?

Intermediate

How to answer in an interview

Assertion functions are functions that throw an error if a condition is false, and TypeScript uses the 'asserts' keyword to narrow types based on the function call. For example, assertIsString(value) tells TypeScript that after the call, value is guaranteed to be a string. They're useful for runtime validation at boundaries like API inputs.

Question 12

Explain the difference between Record, Map, and plain objects for typed keys.

Intermediate

How to answer in an interview

Record<K, V> is a utility type for creating objects with known key types and value types, useful for typed dictionaries. Map<K, V> is a runtime data structure with better performance for frequent additions and deletions, preserving key insertion order. Plain objects with index signatures work for simple cases but lack the type safety and methods of Map.

Question 13

What is declaration merging and how does it work?

Intermediate

How to answer in an interview

Declaration merging is TypeScript's ability to combine multiple declarations with the same name into a single definition. Interfaces merge their members, namespaces merge their exports, and you can augment existing modules or global types using module augmentation or ambient declarations. This is why libraries like Express let you extend Request with custom properties.

Question 14

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

Advanced

How to answer in an interview

'any' disables type checking for a value, so I try to avoid it unless I am migrating legacy code. 'unknown' is safer because the compiler forces me to check and narrow it before using it. 'never' represents a value that should not exist, such as a function that cannot return or an impossible branch in exhaustive checks.

Question 15

Why is strict mode important in a TypeScript project?

Advanced

How to answer in an interview

Strict mode turns on a set of compiler checks that make TypeScript much more effective at finding bugs early, especially around null handling, implicit any usage, and unsafe assumptions. In production projects, I prefer strict mode because it forces clearer contracts, improves refactoring confidence, and prevents many bugs that would otherwise appear only at runtime.

Question 16

What are template literal types in TypeScript?

Advanced

How to answer in an interview

Template literal types let you create new string literal types by combining existing ones using template literal syntax. Combined with intrinsic string manipulation types like Uppercase and Lowercase, they allow compile-time string transformations, which is useful for strongly-typed event names, CSS class builders, or API route definitions where the string pattern matters.

Question 17

How do mapped types work in TypeScript?

Advanced

How to answer in an interview

Mapped types iterate over the keys of an existing type to produce a new type, allowing you to transform each property's type, add or remove modifiers like readonly or optional, or remap keys using the 'as' clause. Built-in types like Partial, Required, and Pick are implemented as mapped types under the hood.

Question 18

Explain conditional types in TypeScript.

Advanced

How to answer in an interview

Conditional types take the form T extends U ? X : Y, allowing you to choose between two types based on whether one type extends another. They enable powerful type-level programming, such as extracting return types, filtering union members, or transforming types based on conditions. When applied to a union, conditional types distribute over each member automatically.

Question 19

What is the infer keyword used for in TypeScript?

Advanced

How to answer in an interview

The infer keyword lets you declare a type variable within a conditional type that TypeScript will try to infer from the checked type. It's commonly used to extract parts of complex types, like the return type of a function, the element type of an array, or the resolved type of a Promise, enabling powerful type-level extraction and transformation.

Question 20

How do you type higher-order functions in TypeScript?

Advanced

How to answer in an interview

Higher-order functions that accept callbacks should use generic type parameters to preserve the relationship between input and output types. For example, a map function should be typed as function map<T, U>(arr: T[], fn: (item: T) => U): U[], so TypeScript infers the output array type from the callback. This avoids losing type information through the transformation.

More in Tech

Keep browsing related topics.

Browse all