Put in order · JavaScript and React course · Module 12: React and TypeScript

Type Narrowing with Discriminated Union

Arrange the steps for creating and using a discriminated union in the correct order:

Items to put in order

  • Use switch on the discriminator field (e.g. switch(obj.kind))
  • Create a union type: type MyType = TypeA | TypeB | TypeC
  • Define separate interfaces with a common field (e.g. kind)
  • TypeScript automatically narrows the type in each case

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