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. Should you use an enum or a union of string literals in TypeScript?

    Both describe a fixed set of allowed values. An enum creates a real JavaScript object at runtime and its members must be referenced by name. A union of string literals like "admin" | "user" exists only at compile time, adds no code, and accepts plain strings. Many teams now prefer unions, or an as const object when they also need runtime values.

    IntermediateIn-depth4 min read
  7. What is type narrowing in TypeScript, and how do type guards work?

    Narrowing is how TypeScript refines a broad type, like string | number, to a more specific one inside a block after a check. Type guards are those checks, such as typeof, instanceof, the in operator, equality checks on a shared field, and custom functions that return a type predicate like value is User.

    IntermediateIn-depth3 min read
  8. What is the never type in TypeScript, and how do you use it for exhaustive checks?

    never is the type with no possible values. It is the return type of functions that never finish normally, like one that always throws, and it is what remains after every case of a union has been handled. Assigning the leftover value to never makes the compiler report any case you forgot.

    AdvancedIn-depth4 min read
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. How do you filter null values out of an array so TypeScript knows the result has no nulls?

    Pass a type predicate to filter: arr.filter((x): x is string => x !== null). Plain filter(x => x !== null) returns (string | null)[] in older TypeScript, because the compiler does not infer the narrowing from the callback.

    IntermediateQuick answer1 min read
  27. How do you get the element type of an array type or a tuple in TypeScript?

    Use an indexed access with number: T[number]. For string[] it gives string, and for a readonly tuple declared as const it gives the union of its member literals.

    IntermediateQuick answer1 min read
  28. How do you remove readonly or optional from every property using mapped type modifiers?

    Prefix the modifier with a minus sign inside a mapped type: -readonly removes readonly and -? removes optionality. A plus sign, or none, adds it. Required<T> is exactly { [K in keyof T]-?: T[K] }.

    IntermediateQuick answer1 min read
  29. How do you write a generic arrow function in a .tsx file?

    In .tsx, <T>(x: T) => x is parsed as a JSX tag and fails. Write <T,>(x: T) => x with a trailing comma, or <T extends unknown>, or use a regular function declaration instead.

    IntermediateQuick answer1 min read
  30. How do you write a type-safe getProperty function with keyof?

    Use two type parameters: function getProperty<T, K extends keyof T>(obj: T, key: K): T[K]. K is limited to real keys of T, and the return type is exactly that property's type, so no casts or any are needed.

    IntermediateQuick answer1 min read