TypeScript · Senior · Question 11 of 75
What does the const modifier on a type parameter (TS 5.0) give you?
function routes<const T extends readonly string[]>(r: T) { return r; }
const r = routes(['home', 'about']);- ✓Correct answer: Literal inference, like
as const:risreadonly ['home', 'about'] - 2A ban on modifying the argument inside the function at runtime
- 3
Tcan't be overridden by an explicit argument when calling
Explanation
Without const, string[] would be inferred, and the caller would have to write as const. The modifier affects only type inference, not runtime. The readonly string[] constraint is needed so an array literal fits as a readonly tuple.