Skip to content

Tuple types are not indexed by number in mapped typesΒ #48398

Closed
@Jamesernator

Description

@Jamesernator

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueWorking as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions