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']);

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.

Further reading

Problem with a question

TypeScript · Senior · question 11

Test yourself

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

Take this quiz