Interview Q&A / TypeScript
TypeScript interview questions
Types, interfaces, generics and safe patterns for real codebases.
Prefer rapid revision? Practise TypeScript short interview answers →
- 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - How do you write a user-defined type guard with the is keyword?
Declare a function whose return type is a type predicate, like (x: unknown): x is User. When it returns true, TypeScript treats x as User in that branch. The compiler trusts your logic, so the check inside must be correct.
IntermediateQuick answer1 min read - How does Record with a union of keys help catch missing cases?
Record<Union, V> requires an entry for every member of the union. Adding a new member later makes every object typed with it fail until the new key is added, so you cannot forget a case.
IntermediateQuick answer1 min read - What does keyof typeof do, and why is it used with objects?
typeof obj gives the type of the object and keyof turns its keys into a union, so keyof typeof obj is the union of the object's key names. It lets one object be the single source for both runtime values and key types.
IntermediateQuick answer1 min read - What does the Parameters utility type do?
Parameters<typeof fn> returns the parameter types of a function as a tuple, for example [string, number]. You can index it, like Parameters<typeof fn>[0], to get one argument's type, which helps when wrapping functions.
IntermediateQuick answer1 min read - What is a conditional type in TypeScript?
A conditional type picks one of two types based on a check: T extends U ? X : Y. If T is assignable to U the result is X, otherwise Y. It lets types make decisions, like an if statement for types.
IntermediateQuick answer1 min read - What is a default type parameter in a generic, and when is it useful?
A default like <T = string> is used when the caller gives no type argument and TypeScript cannot infer one. It keeps simple usage short while still allowing a custom type when needed.
IntermediateQuick answer1 min read - What is a mapped type in TypeScript?
A mapped type builds a new object type by looping over the keys of another type with the syntax { [K in keyof T]: ... }. Partial, Readonly and Pick are all mapped types written this way.
IntermediateQuick answer1 min read - What is the Awaited utility type for?
Awaited<T> gives the type you get after awaiting a promise, unwrapping nested promises too. Awaited<Promise<string>> is string. It is mostly used with ReturnType to get the resolved type of an async function.
IntermediateQuick answer1 min read