TypeScript · Junior · Question 31 of 75
What does as const do in const roles = ['admin', 'user'] as const?
- ✓Correct answer: Makes the array a readonly tuple with the literal types
'admin'and'user' - 2Freezes the array at runtime via
Object.freezeduring compilation - 3Turns the array into an
enumwith string values
Explanation
Without as const, the type is string[]. With it, it's readonly ['admin', 'user'], and you can derive a union: type Role = typeof roles[number] → 'admin' | 'user'. The object isn't frozen at runtime.