Files
scriptc/tests/corpus/6481-enum-runtime-keys.ts
T
Malte UblandClaude Opus 5.5 5e65642fcf Compile mixed-tuple Promise.all, Boolean callbacks, enum key lookups and nullable regex receivers
- `Promise.all([p, q, r])` over different promise types (the tuple
  overload) lowers in TypeScript through one interned async helper that
  awaits the runtime combinator over the same promises (widened to
  Promise<void>, a type-only view), so settlement order and rejection
  handling are Node's, then reads each fulfilled entry into the tuple.
  Entries keep their identity; plain scalar entries pass through, and a
  checked-dynamic entry joins through the promise it holds.
- The ambient `Boolean` as an `every`/`some`/`find`/`findIndex`/`map`
  callback in TypeScript is the interned one-parameter truthiness
  closure `filter(Boolean)` already used, and `filter(Boolean)` accepts
  checked-dynamic elements (runtime truthiness).
- `E[key]` with a runtime string key answers the named member's constant,
  or undefined for a name no member carries; a numeric member's reverse
  key and inherited Object.prototype names throw a named runtime refusal.
- Regex-argument `replace`/`replaceAll`/`split`/`search`/`matchAll` on a
  `string | undefined` receiver take the string lowering and throw Node's
  "Cannot read properties of undefined (reading 'replace')".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 23:16:02 -07:00

30 lines
944 B
TypeScript

// @transform-types
// Enum member lookup through a runtime string key (`E[name as keyof
// typeof E]`): each member name answers its constant — string, numeric and
// renamed members alike — and a name no member carries reads undefined.
enum Color {
red = "red",
"dark-blue" = "dark-blue",
green = "light-green",
}
enum Level {
low,
mid = 5,
high,
}
function color(name: string | undefined): Color | undefined {
return name ? Color[name as keyof typeof Color] : undefined;
}
for (const name of ["red", "dark-blue", "green", "light-green", "", undefined]) {
console.log(JSON.stringify(name), JSON.stringify(color(name)));
}
const levelName = process.argv.length > 99 ? "unused" : "high";
const level: Level | undefined = Level[levelName as keyof typeof Level];
console.log(level, level === Level.high);
const missing: Level | undefined = Level[("med" + "ium") as keyof typeof Level];
console.log(missing === undefined);