Skip to content

Spreading a named tuple slice results in the names being in the wrong positionΒ #55693

Closed
@danny-may

Description

@danny-may

πŸ”Ž Search Terms

named tuple (label:bug)

πŸ•— Version & Regression Information

  • This was introduced in 5.2.2 as prior versions would discard the element names

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/JYOwLgpgTgZghgYwgAgLIFcA2ZgGkICeqcADgDz4EDOyEAHpCACY1QRxMD2ImBy6IANYhOAdxABtALoAaZADU4mdBAB8yAN4BYAFDJ9yAOYQwFQrQYRmNAOInKVM9VWqAFADpPgwgC5klAEo-OzAAJQgqLFMHOUo5RWU1XQBfXTACEhQQhycaekYWZDYObl5+IRFxaXUAXn9CPMtrZAlPd1AYaGRwgFs4UDkOroB9KWRdA2QAfnrqZAAfZGyGsl7+kHUJgz8HAG5dNIysk3DI7DIASSYLAtZ2Lh4+AWExSVlkAEEoQ0bbpfsVldVPElCpap9vr8rIUruM9AYZgkVAtykwIDBQBAmFt9H5YfloTRWp4vj85G0hlBuhEwFIcZNpsgLpAoHAAEaYCBkYnuU5gEGJKSqemTPwgCAAN2g+x0ugQ3CoYGQdD8GGweEIxHIEmATAAjH5FVBQIZBkwAEyGsDGkCGd6cNkAKwgCDA4JAWEwAEIZfKQIrkLq9cg6gBydkIUPIOA0I0m30KpV9MAIAAWERDyvcxjAriDAV2cKAA

πŸ’» Code

interface MultiKeyMap<Keys extends readonly unknown[], Value> {
    get<Key extends GetKeys<Keys>>(...key: Key): GetResult<Keys, Key, Value>
}
type GetKeys<Keys extends readonly unknown[]> = Keys extends [...infer Remain, infer _] 
    ? Keys | GetKeys<Remain> 
    : Keys;

type GetResult<Id extends readonly unknown[], Args extends GetKeys<Id>, Value> = Args extends Id 
    ? Value | undefined
    : Id extends [...Args, ...infer Rest]
        ? Iterable<[...Rest, Value]>
        : never;

const x: MultiKeyMap<[id1: string, id2: string], object> = null!;
const id1 = 'abc' as string;
const matches = x.get(id1); 

πŸ™ Actual behavior

typeof matches = Iterable<[string, id2: object]>

πŸ™‚ Expected behavior

typeof matches = Iterable<[id2: string, object]>

Additional information about the issue

There are a few bits to this bug which I wasnt able to remove from the example. I tried simplifiying it as much as I could, but any further changes I made would result in the bug disappearing and the correct labels being shown. If I had to hazard a guess, I would assume its something relating to this chunk:

Id extends [...Args, ...infer Rest]
        ? Iterable<[...Rest, Value]>

The index of the label captured by the ...infer Rest isnt reduced by the length of Args, so it ends up on element 1 of [...Rest, Value] rather than element 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions