Interview Q&A / TypeScript

INTERVIEW Q&A · 47 QUESTIONS

TypeScript interview questions

Types, interfaces, generics and safe patterns for real codebases.

  1. What are generics in TypeScript and why are they useful?

    Generics let you write a function, class or type that works with many types while still remembering which type was used. Instead of falling back to any, you add a type parameter like T, so the output type is linked to the input type and TypeScript can still check your code.

    BeginnerIn-depth3 min read
  2. What are utility types in TypeScript, such as Partial, Required, Pick, Omit and Record?

    Utility types are built-in generic types that create a new type from an existing one. Partial makes all properties optional, Required makes them all required, Pick keeps only chosen keys, Omit removes chosen keys, and Record builds an object type from a set of keys and a value type.

    BeginnerIn-depth3 min read
  3. What is the difference between interface and type in TypeScript?

    Both describe the shape of data. An interface describes objects and can be extended or merged; a type alias can name any type, including unions, tuples and primitives.

    BeginnerIn-depth2 min read
  4. What is the difference between union and intersection types in TypeScript?

    A union type (A | B) means a value can be one of several types, so you can only use what they have in common until you narrow it. An intersection type (A & B) means a value must be all of the types at once, so it has every property from each.

    BeginnerIn-depth3 min read
  5. What is the difference between unknown and any in TypeScript?

    Both can hold any value. any turns off type checking completely, while unknown forces you to check what the value is before you use it, so it is the safe choice.

    BeginnerIn-depth2 min read
  6. How are null and undefined treated in TypeScript when strictNullChecks is on?

    With strictNullChecks, null and undefined are their own types and are not assignable to string, number or objects unless you say so in a union such as string | null. The compiler then forces you to check before using the value.

    BeginnerQuick answer1 min read
  7. How do you get the return type of an existing function using ReturnType?

    ReturnType<typeof fn> gives the type a function returns. You need typeof because ReturnType expects a type, not a value, and a function name is a value.

    BeginnerQuick answer1 min read
  8. How do you restrict a generic type parameter using extends?

    Write T extends Something to say the caller may pass any type, as long as it has at least the shape of Something. Inside the function you can then safely use those members, such as .length or .id.

    BeginnerQuick answer1 min read
  9. How do you write a generic class in TypeScript, for example a Stack?

    Put the type parameter after the class name, like class Stack<T>, and use T in fields and method signatures. Each instance fixes T when created, so new Stack<number>() only accepts and returns numbers.

    BeginnerQuick answer1 min read
  10. How would you model an API response with a generic type?

    Define one generic interface such as ApiResponse<T> with a data: T field plus shared fields like status and message. Each endpoint then reuses it with its own data type, for example ApiResponse<User[]>.

    BeginnerQuick answer1 min read
  11. What are indexed access types in TypeScript?

    An indexed access type reads the type of a property using bracket syntax, like User["email"]. You can use a union of keys, or number to get an array's element type, as in string[][number].

    BeginnerQuick answer1 min read
  12. What does NonNullable do, and when would you use it?

    NonNullable<T> removes null and undefined from a type, so NonNullable<string | null | undefined> is string. It is useful after you have checked a value, or to type something that must never be empty.

    BeginnerQuick answer1 min read
  13. What does the keyof operator do in TypeScript?

    keyof T produces a union of the property names of type T as string literals. For { name: string; age: number } it gives "name" | "age". It is used to restrict a value to valid keys of an object.

    BeginnerQuick answer1 min read
  14. What does the void type mean, and how is it different from undefined?

    void marks a function whose return value is meant to be ignored. undefined is an actual value. A function typed to return void may still be assigned a function that returns something, but its result cannot be used.

    BeginnerQuick answer1 min read
  15. What is a discriminated union and why is it useful?

    A discriminated union is a union of object types that share a literal-typed property, such as kind or type. Checking that property narrows the whole object to one member, so each branch sees only the fields that belong to it.

    BeginnerQuick answer1 min read
  16. What is a tuple in TypeScript, and how is it different from an array?

    A tuple is an array with a fixed length where each position has its own type, like [string, number]. A normal array type such as (string | number)[] allows any length and any order of those types.

    BeginnerQuick answer1 min read
  17. What is the difference between Exclude and Extract in TypeScript?

    Both work on union types. Exclude<T, U> removes the members of T that are assignable to U, and Extract<T, U> keeps only those members. They filter a union, not an object's properties; for objects use Omit and Pick.

    BeginnerQuick answer1 min read
  18. What is the difference between number and Number in TypeScript?

    number is the primitive type you should use. Number (capital N) is the type of the wrapper object created by new Number() and is almost never what you want; the same applies to string vs String and boolean vs Boolean.

    BeginnerQuick answer1 min read
  19. What is the difference between typeof in JavaScript and typeof in a type position?

    In an expression, typeof returns a string like "number" at runtime. In a type position, typeof x gives the compile-time TypeScript type of the variable x. Same word, different places and different jobs.

    BeginnerQuick answer1 min read
  20. What is the difference between typeof, instanceof and the in operator for narrowing?

    typeof narrows primitives like string and number, instanceof narrows class instances by checking the prototype chain, and in narrows object types by checking whether a property name exists. Pick the one that matches what makes the types different.

    BeginnerQuick answer1 min read
  21. What is the pitfall of narrowing with a truthiness check in TypeScript?

    if (x) removes null and undefined but also removes valid falsy values: 0, an empty string, false and NaN. For a number or string that could legitimately be empty, check against null and undefined explicitly.

    BeginnerQuick answer1 min read
  22. What is type inference in TypeScript, and when should you still write type annotations?

    Inference means the compiler works out a type from the value you assign, so you do not write it. Annotate function parameters, public return types and variables declared without a value, where inference has nothing to go on or where you want a contract.

    BeginnerQuick answer1 min read