Const Assertion for Literal Values
In this lesson, we will see how to define a lilteral value with the "as const" keyword.
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, this change is done only to the literal type. Here is an example:
Get hands-on with 1400+ tech skills courses.