Literal types and const assertions

Widening is why your string turned into string. A const assertion keeps the literal, which is what makes union-of-literals modelling work.

Lesson 1.35m 38sIntermediate19.8k students

Chapters

5:38
  1. 0:00SO underrated
  2. 0:12Objects are mutable!
  3. 1:54How as const works
  4. 2:38Object.freeze
  5. 3:19Deriving types from as const
  6. 4:30keyof typeof

What this lesson covers

  • Understand literal widening and when it happens
  • Freeze a literal shape with as const
  • Derive a union of allowed values from a constant array

This lesson sits in How the Type System Thinks, part of TypeScript for Application Developers. It assumes what came before it and leads directly into the next lesson in the module.

In this lesson you will

  • Understand literal widening and when it happens
  • Freeze a literal shape with as const
  • Derive a union of allowed values from a constant array

Resources