TypeScript · Junior · Question 31 of 75

What does as const do in const roles = ['admin', 'user'] as const?

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.

Further reading

Problem with a question

TypeScript · Junior · question 31

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz