Mapped & Conditional Types Questions with Answers

17 practice questions on Mapped & Conditional Types (TypeScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Easy

What is IsStr<number>?

type IsStr<T> = T extends string ? "yes" : "no";
  1. "yes" | "no"
  2. "yes"
  3. boolean
  4. "no"

Answer: D. "no"

Explanation

A conditional type picks the true branch when T is assignable to string. number is not, so the result is "no".

Step-by-step solution

  1. number does not extend string.
  2. The false branch is chosen.
  3. The result is "no".

Why the other options are wrong

  • A. That is for boolean.
  • B. number is not a string.
  • C. Not boolean.

Tip: Check if T extends U.

Question 2Easy

What is C?

const cfg = { port: 3000, host: "localhost" };
type C = typeof cfg;
  1. "object"
  2. { port: 3000; host: "localhost" }
  3. { port: number; host: string }
  4. object

Answer: C. { port: number; host: string }

Explanation

In a type position, typeof gives the static type of a variable. Properties of a normal object literal widen to number and string.

Step-by-step solution

  1. Cfg is an object literal with port: 3000 and host: 'localhost'.
  2. In a type position, typeof cfg gives the static type of cfg.
  3. Object literal properties widen, so port is number and host is string.
  4. The answer is option C.

Why the other options are wrong

  • A. Typeof gives the object type here, not the string 'object'.
  • B. Literal values are widened to number and string for a plain object literal.
  • D. Object is too general; the type keeps the property names and types.

Tip: Object literal properties widen to number or string unless the object is declared as const.

Question 3Easy

What is keyof { id: number; name: string }?

  1. ["id", "name"]
  2. "id" | "name"
  3. number | string
  4. string

Answer: B. "id" | "name"

Explanation

keyof produces a union of an object type's property names as string literal types.

Step-by-step solution

  1. Keyof gives the property names of the object type.
  2. Here the names are id and name.
  3. They are written as a union of string literals.
  4. The answer is option B.

Why the other options are wrong

  • A. Keyof gives a union, not a tuple.
  • C. Keyof gives the names as string literals, not the value types.
  • D. Keyof gives the literal names, not the general string type.

Tip: Keyof T is a union of the property names of T.

Question 4Easy

In let y: typeof x;, what does typeof do?

  1. Copies x's compile-time type to y
  2. Checks the type of x once the program runs
  3. Returns a string such as "number" at runtime
  4. Makes y a reference to the variable x

Answer: A. Copies x's compile-time type to y

Explanation

In a type annotation, typeof is a type query evaluated by the compiler. The JavaScript typeof operator is a separate, runtime feature.

Step-by-step solution

  1. In a type position, typeof x reads the compile-time type of the variable x.
  2. The statement let y: typeof x; gives y the same type as x.
  3. This is a compile-time check and emits no runtime code.
  4. So it copies x's compile-time type to y (option A).

Why the other options are wrong

  • B. The type check happens at compile time, not when the program runs.
  • C. That describes the runtime typeof operator, not the type query.
  • D. Y is a separate variable with the same type, not a reference to x.

Tip: In a type annotation typeof is a type query; in an expression it is the runtime operator.

Question 5Medium

What is Role?

const roles = ["admin", "user"] as const;
type Role = typeof roles[number];
  1. "admin" | "user"
  2. string[]
  3. readonly ["admin", "user"]
  4. string

Answer: A. "admin" | "user"

Explanation

as const makes the array a readonly tuple of literals. Indexing its type with number gives the union of the element types.

Step-by-step solution

  1. As const makes roles a readonly tuple of literals: readonly ['admin', 'user'].
  2. Typeof roles gives that tuple type.
  3. Typeof roles[number] reads the element types of the tuple.
  4. The union 'admin' | 'user' is the answer (option A).

Why the other options are wrong

  • B. string[] would be the type without as const, and the literals are kept here.
  • C. That is the tuple itself, not the union of its elements.
  • D. As const keeps the literal types, so the element type is not plain string.

Tip: Use as const together with T[number] to derive a union type from an array of values.

Question 6Medium

What does the -readonly modifier do in this mapped type?

type Mutable<T> = { -readonly [K in keyof T]: T[K] };
  1. Deletes properties that are readonly
  2. Makes every property optional
  3. Adds readonly to each property
  4. Removes readonly from each property

Answer: D. Removes readonly from each property

Explanation

Mapped type modifiers can be added with + (the default) or removed with -. -readonly strips the readonly modifier.

Step-by-step solution

  1. The - prefix removes a modifier.
  2. -readonly removes readonly.
  3. Properties become mutable.

Why the other options are wrong

  • A. No properties are deleted.
  • B. That is -? or Partial.
  • C. That is +readonly.

Tip: -readonly strips readonly.

Question 7Medium

What is EventName?

type EventName = `on${"Click" | "Hover"}`;
  1. string
  2. "onClickHover"
  3. "onClick" | "onHover"
  4. on${string}

Answer: C. "onClick" | "onHover"

Explanation

A template literal type applied to a union produces one string literal per member.

Step-by-step solution

  1. A template literal type applied to a union gives one string for each member.
  2. 'on' followed by Click gives 'onClick'.
  3. 'on' followed by Hover gives 'onHover'.
  4. The result is 'onClick' | 'onHover' (option C).

Why the other options are wrong

  • A. The type is narrower than string; it is only those two literals.
  • B. It is a union of two strings, not one combined string.
  • D. That pattern allows any suffix, but here the suffix is only Click or Hover.

Tip: A template literal over a union expands to the union of every combination.

Question 8Medium

What is Unwrap<number>?

type Unwrap<T> = T extends Promise<infer V> ? V : T;
  1. never
  2. Promise<number>
  3. number
  4. unknown

Answer: C. number

Explanation

number is not a Promise, so the false branch returns T unchanged. Unwrap<Promise<string>> would give string.

Step-by-step solution

  1. Unwrap checks whether T extends Promise of infer V.
  2. Number is not a Promise, so the condition is false.
  3. The false branch returns T unchanged, which is number.
  4. The answer is number (option C).

Why the other options are wrong

  • A. Never only comes from a never input, which is not used here.
  • B. Promise<number> is the input type, not the result for number.
  • D. Unknown is not returned; T is number.

Tip: A conditional type with infer unwraps only when the pattern matches; otherwise the false branch applies.

Question 9Hard

What does TypeScript report for the last line?

type ToArr<T> = T extends any ? T[] : never;
const x: ToArr<string | number> = [1, "a"];
  1. Error: the type is string[] | number[], so mixing fails
  2. No error; the type is any[]
  3. No error; the type is (string | number)[]
  4. Error: the type is never

Answer: A. Error: the type is string[] | number[], so mixing fails

Explanation

Conditional types distribute over a union when the checked type is a naked type parameter, giving string[] | number[]. A mixed array fits neither member.

Step-by-step solution

  1. ToArr is a conditional type on a bare type parameter, so it distributes over the union.
  2. String | number becomes string[] | number[].
  3. The array [1, 'a'] has element type string | number, which fits neither string[] nor number[] alone.
  4. So TypeScript reports an error (option A).

Why the other options are wrong

  • B. No error: a mixed array does not fit either member of the union.
  • C. The type is string[] | number[], not (string | number)[].
  • D. The type is not never; the union is valid, and the assigned value is the problem.

Tip: Conditional types distribute over unions; wrap the checked type in brackets, as [T] extends [any], to stop that.

Question 10Hard

What is keyof { [k: string]: number }?

type K = keyof { [k: string]: number };
  1. string | number
  2. number
  3. never
  4. string

Answer: A. string | number

Explanation

A string index signature also accepts numeric keys, because JavaScript converts obj[1] to obj["1"]. So keyof includes number.

Step-by-step solution

  1. The index signature [k: string]: number accepts string keys.
  2. JavaScript converts numeric keys to strings, so number keys also work on it.
  3. Keyof therefore includes both string and number.
  4. The answer is string | number (option A).

Why the other options are wrong

  • B. String keys are also included, so number alone is not enough.
  • C. A string index signature gives a non-empty key type.
  • D. Number keys are also allowed, so number must be in the union.

Tip: Keyof of a string index signature is string | number.

Practise all 17 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all TypeScript topics · All topics