Unions & Narrowing Questions with Answers

17 practice questions on Unions & Narrowing (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

Inside the final return statement, what is the type of x?

function size(x: string | number) {
  if (typeof x === "string") return x.length;
  return x.toFixed(0);
}
  1. unknown
  2. number
  3. string
  4. string | number

Answer: B. number

Explanation

The typeof check returns early for strings, so control flow analysis narrows x to number for the remaining code.

Step-by-step solution

  1. The if returns early when x is a string.
  2. After that line, x can only be number.
  3. So x.toFixed(0) is called on a number, and x has type number.
  4. The answer is option B.

Why the other options are wrong

  • A. Unknown is not the result of the typeof check; narrowing gives number.
  • C. X is a string only inside the if block, not after it.
  • D. The union was narrowed by the early return, so it is not string | number here.

Tip: An early return after a typeof check narrows the rest of the function.

Question 2Easy

What does TypeScript report for this function?

function shout(x: string | number) {
  return x.toUpperCase();
}
  1. Error: toUpperCase does not exist on type 'string | number'
  2. No error; x is treated as a string
  3. Error: union types cannot be function parameters
  4. No error; the call is checked at runtime

Answer: A. Error: toUpperCase does not exist on type 'string | number'

Explanation

On a union, you can only use members that exist on every member type. Narrow with typeof x === "string" before calling string methods.

Step-by-step solution

  1. x is string
  2. number, so only members common to both are allowed.
  3. toUpperCase exists on string but not on number.
  4. TypeScript reports an error.

Why the other options are wrong

  • B. x is not narrowed here.
  • C. Unions are allowed as parameters.
  • D. Type checking happens at compile time, not runtime.

Tip: Union: narrow before using specific methods.

Question 3Easy

What makes the union below a discriminated (tagged) union?

type Shape =
  | { kind: "circle"; r: number }
  | { kind: "square"; side: number };
  1. Every member has at least one numeric property
  2. The union is declared with the type keyword
  3. Each member has a kind with a distinct literal type
  4. Its members are listed on separate lines with |

Answer: C. Each member has a kind with a distinct literal type

Explanation

A shared property with distinct literal types (the discriminant) lets a check like s.kind === "circle" narrow s to one member.

Step-by-step solution

  1. A discriminated union has a shared property with a different literal type in each member.
  2. Here kind is 'circle' in one member and 'square' in the other.
  3. A check on s.kind narrows s to one member.
  4. So the answer is option C.

Why the other options are wrong

  • A. Numeric properties do not make a union discriminated.
  • B. The type keyword is only how the union is declared; it is not what makes it tagged.
  • D. Line breaks and | separators are only layout, not the tag.

Tip: A discriminant is a shared property with distinct literal types, such as kind.

Question 4Easy

What does TypeScript report for this line?

let dir: "up" | "down" = "left";
  1. Error: "left" is not assignable to type '"up" | "down"'
  2. No error, but dir is widened to string
  3. No error; any string is accepted
  4. Error: string literals cannot be used as types

Answer: A. Error: "left" is not assignable to type '"up" | "down"'

Explanation

A union of string literal types allows only those exact values, so it works like a lightweight enum.

Step-by-step solution

  1. The annotation says dir can only be the literal 'up' or 'down'.
  2. The value 'left' is not in that set.
  3. So TypeScript reports an error saying 'left' is not assignable.
  4. The answer is option A.

Why the other options are wrong

  • B. Dir is not widened; the annotation keeps it as the union.
  • C. Only the two literals are allowed, not any string.
  • D. String literals are valid as types, so there is no such error.

Tip: A union of string literals works like an enum-style set of allowed values.

Question 5Medium

Inside the if block, what is the type of pet?

type Fish = { swim(): void };
type Bird = { fly(): void };
function move(pet: Fish | Bird) {
  if ("swim" in pet) {
    pet.swim();
  }
}
  1. Fish | Bird
  2. Fish
  3. Bird
  4. object

Answer: B. Fish

Explanation

The in operator narrows a union to the members that declare (or optionally declare) that property. Only Fish has swim.

Step-by-step solution

  1. The 'swim' in pet check tests whether the property exists on the value.
  2. Only Fish declares swim, so the union narrows to Fish.
  3. Inside the if block, pet is Fish, so pet.swim() compiles.
  4. So the answer is option B, Fish.

Why the other options are wrong

  • A. Fish | Bird is the type outside the if block, not inside it.
  • C. Bird has fly, not swim, so Bird is removed.
  • D. Object is wider than needed; the in check narrows to Fish.

Tip: The in operator narrows a union to the members that declare that property.

Question 6Medium

What does the return type pet is Fish do here?

type Fish = { swim(): void };
type Bird = { fly(): void };
function isFish(pet: Fish | Bird): pet is Fish {
  return (pet as Fish).swim !== undefined;
}
  1. A true result narrows the argument to Fish for the caller
  2. It throws an error when the argument is not a Fish
  3. It makes the function return either a Fish object or false
  4. It converts the argument into a Fish at runtime

Answer: A. A true result narrows the argument to Fish for the caller

Explanation

A type predicate tells the compiler that when the function returns true, the argument has the stated type. The runtime return value is still just a boolean.

Step-by-step solution

  1. Pet is Fish is a type predicate on the return type.
  2. It tells the compiler that when the function returns true, the argument is a Fish.
  3. The caller then sees the narrowed type in the true branch.
  4. So the answer is option A.

Why the other options are wrong

  • B. The function returns a boolean; it does not throw when the argument is not a Fish.
  • C. The function returns true or false, not a Fish object or false.
  • D. No conversion happens at runtime; the predicate only informs the compiler.

Tip: A type predicate narrows types for the caller; the runtime result is still a plain boolean.

Question 7Medium

What does the ! in document.getElementById("app")! do?

  1. Drops null and undefined from the type, with no runtime check
  2. Throws an error at runtime when the element is missing
  3. Makes the compiler check the DOM at build time for the element
  4. Converts the result to a boolean value

Answer: A. Drops null and undefined from the type, with no runtime check

Explanation

The non-null assertion operator only affects type checking. If the element really is missing, the code fails later at runtime.

Step-by-step solution

  1. GetElementById returns an element or null.
  2. The! operator tells the compiler to treat the value as non-null.
  3. It affects only type checking, with no runtime check, so a missing element fails later.
  4. The answer is option A.

Why the other options are wrong

  • B. The! operator adds no runtime check, so it does not throw by itself.
  • C. The compiler does not check the DOM at build time; the operator only changes the type.
  • D. The! operator does not convert the value to a boolean.

Tip: The! operator is a type-level promise, not a runtime check; use an if check when the value may be missing.

Question 8Medium

What is the inferred return type of toList?

function toList(x: string | string[]) {
  return Array.isArray(x) ? x : [x];
}
const r: string[] = toList("a");
  1. any[]
  2. string[]
  3. (string | string[])[]
  4. string | string[]

Answer: B. string[]

Explanation

Array.isArray narrows x to string[] in the true branch and to string in the false branch, where [x] is string[]. Both branches give string[].

Step-by-step solution

  1. Array.isArray narrows x to string[] in the true branch, so the result is x, a string[].
  2. In the false branch, x is string, so [x] is a string[].
  3. Both branches give string[], so the inferred return type is string[].
  4. The answer is option B.

Why the other options are wrong

  • A. any[] would need an untyped value; the branches are typed.
  • C. The result is not an array of mixed items; both branches are string[].
  • D. The return type is an array, not the union of the two input types.

Tip: Array.isArray narrows a union to the array member in the true branch.

Question 9Hard

What does TypeScript report for the line inside the if block?

function f(x: object | string | null) {
  if (typeof x === "object") {
    const o: object = x;
  }
}
  1. No error; null is assignable to object
  2. Error: x may be null, since typeof null is "object"
  3. No error; x is narrowed to object
  4. Error: type string is not assignable to type object

Answer: B. Error: x may be null, since typeof null is "object"

Explanation

typeof null === "object" in JavaScript, so this check narrows x to object | null. Add x !== null to narrow it to object.

Step-by-step solution

  1. Typeof x === 'object' narrows x to object or null, because typeof null is also 'object'.
  2. So inside the if block, x has type object | null.
  3. Assigning that to a variable of type object fails, since null is not an object.
  4. So the answer is option B, an error because x may be null.

Why the other options are wrong

  • A. Null is not assignable to object under strict null checks, so there is an error.
  • C. The narrowing keeps null, so x is not narrowed to object alone.
  • D. String is removed by the typeof check, so the message about string does not apply.

Tip: Typeof null is 'object', so add x !== null when you need the object type only.

Question 10Hard

Inside the if block, what is the type of x?

function f(x: string | number, y: string | boolean) {
  if (x === y) {
    return x.toUpperCase();
  }
}
  1. string | number | boolean
  2. string
  3. string | number
  4. never

Answer: B. string

Explanation

For x === y to be true, both must have a type they share. string is the only common member, so both x and y narrow to string.

Step-by-step solution

  1. The check x === y is true only when both values are equal.
  2. X is string or number, and y is string or boolean.
  3. The only type they can share is string, so both narrow to string in the if block.
  4. So x is string, and x.toUpperCase() compiles. The answer is option B.

Why the other options are wrong

  • A. The narrowing removes number and boolean, so x is not the full union.
  • C. Number is not a possible match for y, so it is removed.
  • D. X is not never; string is a valid type shared by both.

Tip: An equality check narrows both sides to the types they can share.

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