Interview Q&A / TypeScript

IntermediateQuick answer1 min readUpdated 3 Oct 2026

What is a conditional type in TypeScript?

SHORT ANSWER

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.

Explain it simply

A bus conductor asks "senior citizen?" If yes, concession fare; if not, full fare. A conditional type asks a question about a type and answers with one of two types.

The question is always "is T assignable to U?" written as T extends U. Conditional types become powerful with generics, because the answer depends on whatever type the caller supplies.

Example

ts
type IsString<T> = T extends string ? "yes" : "no";
type A = IsString<"hello">;   // "yes"
type B = IsString<42>;        // "no"

// Practical: unwrap an array, else keep as is
type Flatten<T> = T extends any[] ? T[number] : T;
type F1 = Flatten<string[]>;  // string
type F2 = Flatten<number>;    // number

// Function return depends on input type
type IdOrName<T extends boolean> = T extends true ? number : string;
function lookup<T extends boolean>(byId: T): IdOrName<T> {
  return (byId ? 101 : "Asha") as IdOrName<T>;
}

What to say in the interview

"A conditional type is T extends U ? X : Y: it resolves to X when T fits U, otherwise Y. It lets the output type depend on an input type, which is how utilities like Exclude, NonNullable and ReturnType are built. Inside generic functions the compiler cannot resolve it, so a cast is often needed for the return value."

#conditional-types

Related questions

Practise more TypeScript questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open TypeScript quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.