Interview Q&A / TypeScript

INTERVIEW Q&A · 47 QUESTIONS

TypeScript interview questions

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

  1. What is the difference between object, Object and {} in TypeScript?

    object means any non-primitive (arrays, functions, plain objects). {} means any value that is not null or undefined, including primitives like 5. Object is the wrapper interface with toString and friends and behaves almost like {}. Prefer real shapes or Record instead.

    IntermediateQuick answer1 min read
  2. When does TypeScript infer generic type arguments, and when must you pass them yourself?

    TypeScript infers type arguments from the values you pass. You must write them explicitly when no argument carries the type, such as an empty collection or a function whose type appears only in the return, or when you want a wider type than the inferred one.

    IntermediateQuick answer1 min read
  3. When should you use a generic instead of a union type for a function parameter?

    Use a generic when the output type must follow the input type, such as returning the same kind you received. Use a union when the function accepts several kinds and the result does not depend on which one it was.

    IntermediateQuick answer1 min read
  4. Why does TypeScript infer a literal type for const but a wider type for let?

    A const can never change, so its type is the exact value, like "IN". A let can be reassigned, so TypeScript widens it to the general type, string. This is called literal widening and it explains many surprising errors with object properties.

    IntermediateQuick answer1 min read
  5. Why does TypeScript sometimes forget a narrowed type inside a callback?

    Narrowing holds only while the compiler can be sure the variable has not changed. A let variable captured by a callback might be reassigned before the callback runs, so TypeScript resets it to the declared type. Use const or copy the value to a const first.

    IntermediateQuick answer1 min read
  6. How do you rename keys in a mapped type using the as clause?

    Add as after the key in a mapped type, like { [K in keyof T as `get${Capitalize<K & string>}`]: () => T[K] }. The expression after as produces the new key name, and mapping a key to never removes it.

    AdvancedQuick answer1 min read
  7. What does the infer keyword do in a conditional type?

    infer declares a type variable inside the extends clause of a conditional type, letting TypeScript capture part of the matched type. ReturnType is written as T extends (...args: any) => infer R ? R : never.

    AdvancedQuick answer1 min read
  8. What is a distributive conditional type?

    When a conditional type is applied to a naked type parameter that is a union, it runs once per union member and joins the results. IsString<string | number> becomes IsString<string> | IsString<number>. Wrapping in [T] turns this off.

    AdvancedQuick answer1 min read
  9. What is an assertion function in TypeScript?

    An assertion function has a return type like asserts x is string. If it returns normally, TypeScript narrows x for the rest of the scope; if the check fails it must throw. It works like a guard that replaces an if block.

    AdvancedQuick answer1 min read