Question · JavaScript and React course · Module 12: React and TypeScript

Discriminator Field in Union Type

What type must the discriminator field (e.g. kind) be in a discriminated union for TypeScript to automatically narrow the type?

Answer options

  • Any object type
  • A literal type (e.g. 'success' | 'error'), so that each variant has a unique value
  • Boolean type - true or false
  • String or number type (but not a literal type)

You can check the correct answer by solving this task in the CodeWorlds game.