Lesson 1.3

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.

Duration
6m
Level
Intermediate
Students
20k students

Overview

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

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

TypeScript handbook

The official reference for the tools used in this lesson.