CodePark/ questions / Generics
START PRACTICINGPRACTICE
Frontend question · TypeScript › Generics

What is the type of r?

function routes<const T extends readonly string[]>(paths: T) {
  return paths;
}
const r = routes(["/", "/about"]);
readonly ["/", "/about"]

The answer is readonly ["/", "/about"]. A const type parameter (added in TypeScript 5.0) infers the argument as if you wrote it with as const. So the literal tuple, with its exact strings, is kept.

string[] is the tempting wrong answer. You would get string[] only without the const modifier.