Const Assertion for Literal Values
Understand how to apply TypeScript's const assertion to make literal values immutable in arrays and objects. Explore the difference between const and as const declarations and learn how to ensure immutability at design time without altering runtime behavior.
We'll cover the following...
We'll cover the following...
With TypeScript version 3.4, we now have the option to define literals with as const. Const assertion is useful when creating an immutable variable.
const vs as const
At first, as const may seem redundant because it is possible to declare a variable with const and make the value be unchangeable (line 1).
However, const and as const differ. With as const (line 3), the declaration is done with let, allowing the value to be changed. But, ...