Unleashing the const Type Parameters
Learn what are const type parameters and how they provide type safety.
We'll cover the following
Introduction to the const
type parameter
The const
type parameters allow us to declare values with const
. TypeScript can now infer the type, not just the literal values.
The const
type parameters allow us to clearly state our intentions in our code. If a variable is supposed to be a constant that never changes, the const
type parameters give us a safety net, ensuring it stays constant, come what may.
Previous versions of TypeScript have supported using as const
to declare an object or array as readonly
. Once TypeScript knows that an object or array isn’t changed at runtime, it can be more confident in its assumptions about the type.
Consider the following two arrays:
Get hands-on with 1400+ tech skills courses.