Skip to content
Laviyx Learn

Try

TypeScript Interview Questions

Frequently asked TypeScript interview questions on types and inference, interfaces, unions and narrowing, generics, utility types, type guards, assertions and configuration, with hints, tested answers and the reasoning interviewers look for.

12 questions · Updated

  1. 1.What is TypeScript, and do its types exist at runtime?

    BeginnerConceptBasics
    Show hint

    What does the compiler output?

    Show solution

    TypeScript is JavaScript with static types. The compiler checks the types and removes them, outputting plain JavaScript. Types do not exist at runtime, so runtime checks (and validation of outside data) still need JavaScript code such as type guards.

  2. 2.What is the difference between interface and type?

    BeginnerConceptTypes
    Show hint

    Merging, and what each can describe.

    Show solution

    Both describe object shapes. Interfaces can be extended with extends and merge when declared twice. Type aliases can name any type, including unions, tuples and function types, and compose with &. A common convention: interfaces for object shapes, type aliases for everything else.

  3. 3.What is the difference between any and unknown?

    BeginnerConceptTypes
    Show hint

    Which one forces a check?

    Show solution

    Both accept any value. any disables checking entirely. unknown must be narrowed before use, so it is the safe type for JSON, API responses and caught errors.

  4. 4.How does TypeScript narrow a union type?

    IntermediateCodingNarrowing
    Show hint

    Ordinary JavaScript checks.

    Show solution

    By following checks such as typeof, instanceof, in, equality, truthiness and custom type guards:

    TypeScript
    function format(v: string | number): string {
      return typeof v === "number" ? v.toFixed(1) : v.toUpperCase();
    }
    console.log(format(2), format("ok"));
    Output
    2.0 OK
  5. 5.What is a discriminated union and how do you make a switch exhaustive?

    IntermediateConceptUnions
    Show hint

    A shared literal property, and never.

    Show solution

    A union of object types sharing a literal property (kind, type, status) that identifies each member, so checking it narrows to one member. Assigning the value to a never variable in the default branch makes the compiler report any unhandled member.

  6. 6.What are generics and why use them instead of any?

    IntermediateCodingGenerics
    Show hint

    Keep the type, not lose it.

    Show solution

    Generics add type parameters so code works with many types while preserving which one is used:

    TypeScript
    function first<T>(items: T[]): T | undefined {
      return items[0];
    }
    const n = first([10, 20]);
    console.log((n ?? 0) + 1);
    Output
    11

    With any, the result would be untyped and errors would go unnoticed.

  7. 7.How do you write a type-safe getProperty function?

    IntermediateCodingGenerics
    Show hint

    K extends keyof T.

    Show solution
    TypeScript
    function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
      return obj[key];
    }
    const user = { name: "Asha", age: 21 };
    console.log(getProperty(user, "age") + 1);
    Output
    22

    Passing a key that does not exist is a compile error, and the return type is the exact property type.

  8. 8.Name some utility types and when you would use them.

    IntermediateConceptUtility types
    Show hint

    Update payloads, safe user objects, lookup tables.

    Show solution

    Partial<T> for updates, Pick and Omit to select or remove properties (such as Omit<User, "password">), Readonly, Record<K, V> for dictionaries, Exclude/Extract/NonNullable for unions, and ReturnType, Parameters and Awaited to reuse function types.

  9. 9.What is a type guard?

    IntermediateConceptNarrowing
    Show hint

    value is Type.

    Show solution

    A function whose return type is a type predicate such as value is User. When it returns true, TypeScript narrows the value to that type. TypeScript trusts the predicate, so its runtime checks must be correct. Guards are how unknown data becomes typed data.

  10. 10.What is the difference between as and satisfies?

    AdvancedConceptAssertions
    Show hint

    Which one checks, and which keeps the inferred type?

    Show solution

    as T tells the compiler to treat a value as T without checking it (only obvious mismatches are rejected). satisfies T checks that the value matches T while keeping its precise inferred type. satisfies is safe; as makes you responsible.

  11. 11.How would you implement ReturnType?

    AdvancedCodingAdvanced types
    Show hint

    A conditional type with infer.

    Show solution
    TypeScript
    type MyReturnType<F> = F extends (...args: never[]) => infer R ? R : never;
    const make = () => ({ id: 1 });
    const value: MyReturnType<typeof make> = { id: 2 };
    console.log(value.id);
    Output
    2

    infer R captures the return type when F matches a function type.

  12. 12.Which compiler options would you enable on a new project?

    AdvancedDesignConfiguration
    Show hint

    strict, and a few more.

    Show solution

    strict (including strictNullChecks and noImplicitAny), noUncheckedIndexedAccess, noImplicitOverride, verbatimModuleSyntax and skipLibCheck, plus the right module and moduleResolution for the runtime (nodenext for Node.js, bundler with a bundler).

Review the lessons