Const Assertion
Question
What does "as const" do?
Hint
as const narrows types to their most specific literal.
Explanation
The "as const" assertion makes a value deeply readonly and narrows types to their most specific literal types. const x = [1, 2] as const gives type readonly [1, 2] (not number[]). const obj = { kind: "circle" } as const gives { readonly kind: "circle" }. This is useful for: 1) Creating literal tuple types from arrays. 2) Creating discriminated unions from objects. 3) Preventing widening of string/number literals. as const is zero-cost (no runtime effect) and purely a type-level operation.
Related Resources
Related Lessons
- Type Annotations
Learn annotations.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.