Q22. What does as const do?
as const narrows literals and makes object properties and arrays readonly in the type system. It is useful for deriving unions from constant objects. It does not freeze values at runtime.
A constant object with as const retains values such as "ready" instead of widening them to string.
Note
Interview approach: define the concept, explain why it matters, then mention one trade-off or limitation.
TypeScript concept
// Illustrative TypeScript contract
type Identifier = string;
const id: Identifier = "item-1";💡 The example demonstrates the compile-time contract discussed in this question.
Key Takeaways
- Explain the concept in your own words.
- Know the practical trade-off and its limits.
- Use a small example to demonstrate the idea.
Knowledge Check
Does `as const` call Object.freeze?