Put in order · JavaScript and TypeScript course · Module 6: TypeScript Basics

Narrowing a Union Type

Arrange the steps of narrowing a union type in TypeScript:

Items to put in order

  • Declaring a variable with a union type (string | number)
  • Safe use of methods of the specific type
  • TypeScript automatically narrows the type in the if block
  • Using typeof to check the type at runtime

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