Complete worked example
typescript
function choose<const C extends string>(choices: readonly C[], preferred: NoInfer<C>): C {
if(!choices.includes(preferred)) throw new Error("Unsupported choice");
return preferred;
}
const color = choose(["red", "blue"], "red");
const numbers = [0, 2, undefined].filter(value => value !== undefined);- Reasoning
- Only choices contribute the vocabulary; the runtime membership rule remains independently enforced.