QuizzesUnions, Narrowing and Discriminated Unions — Concept Check
intermediate
javascript

Unions, Narrowing and Discriminated Unions — Concept Check

Questions on Unions, Narrowing and Discriminated Unions. Unions model "one of these", and narrowing is how the compiler follows your checks. Discriminated unions are the single most useful pattern in the language.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Unions, Narrowing and Discriminated Unions — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.Given a variable `x` of type `string | number | null`, what will the following code return if `x` is `null`? `if (x === null) return 'none'; else if (typeof x === 'string') return x.trim(); else return x.toFixed(2);`

    • AError: Cannot read property 'toFixed' of null
    • BAn empty string
    • C'none'
    • D'null'
  2. 2.A function receives a parameter of type `RequestState`. What happens if the function does not handle all possible `status` values in a switch statement?

    • AThe function will compile but may behave unexpectedly.
    • BThe function will not compile due to missing cases.
    • CThe function will automatically handle missing cases.
    • DThe function will throw a runtime error.
  3. 3.In TypeScript, why is using a discriminated union beneficial when modeling a request state?

    • AIt increases runtime performance.
    • BIt simplifies the code by reducing type checks.
    • CIt allows combining multiple states easily.
    • DIt prevents representing impossible states.
  4. 4.What does the following code do? `if (Array.isArray(x)) { /*...*/ }`

    • ACheck if `x` is not null or undefined.
    • BCheck if `x` is an object.
    • CNarrow down `x` to an array type.
    • DNarrow down `x` to a string type.
  5. 5.A developer uses `if (count)` to check if `count` is defined. What potential issue might arise?

    • AIt throws an error if `count` is `null`.
    • BIt always returns `true` for numbers.
    • CIt only checks for `null`.
    • DIt excludes `0` as well as `undefined`.
  6. 6.What is the purpose of a custom type guard in TypeScript?

    • ATo enforce runtime type checks.
    • BTo define a new type.
    • CTo automatically convert types.
    • DTo narrow down a type when built-ins can't express it.
  7. 7.A developer adds a new status `{ status: 'cancelled' }` to `RequestState`. What must they do to ensure the code compiles?

    • AAdd default cases to all functions.
    • BNo changes are needed.
    • CUpdate all switch statements handling `RequestState`.
    • DRemove the `cancelled` status.
  8. 8.What is a key characteristic of discriminated unions in TypeScript?

    • AThey improve performance by reducing runtime checks.
    • BThey use a shared literal field to differentiate types.
    • CThey allow multiple types to be combined.
    • DThey automatically infer types without checks.