Closed
Description
Bug Report
π Search Terms
tuple, mapped types, number
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about tuple types
β― Playground Link
Playground link with relevant code
π» Code
type ValType = "i32" | "i64";
class Local<Type extends ValType> {
readonly #type: Type;
constructor(type: Type) {
this.#type = type;
}
get type(): Type {
return this.#type;
}
}
type Locals<Types extends readonly ValType[]> = {
[K in keyof Types]: K extends number ? Local<Types[K]> : Types[K]
}
function toLocals<Types extends readonly ValType[]>(valTypes: Types): Locals<Types> {
return valTypes.map(type => new Local(type)) as unknown as Locals<Types>;
}
const i32x2 = ["i32", "i32"] as const;
// Error, the function should've returned the correct type
const locals: readonly [Local<"i32">, Local<"i32">] = toLocals(i32x2);
π Actual behavior
The tuple keys are not mapped correctly as numbers. For some reason K extends number
is never triggered for the numeric keys in ["i32", "i32"]
.
π Expected behavior
The mapping should set K = 0
and K = 1
in the tuple, while it does correctly set them to "0"
and "1"
as far as I can tell there is no way to identify all "numeric like" strings in a mapped type. My assumption was the whole point of allowing "number" keys for objects was so that this kind've thing worked.
Notes
The mapped types still work fine for array, i.e. Locals<readonly ("i32")[]>
resolves just fine to readonly Local<"i32">[]
but this functionality doesn't extend to tuples.