Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remapping a tuple constraint fails to provide a contextual type for the tuple elements #52650

Closed
Andarist opened this issue Feb 7, 2023 · 0 comments Β· Fixed by #52651
Closed
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@Andarist
Copy link
Contributor

Andarist commented Feb 7, 2023

Bug Report

πŸ”Ž Search Terms

tuple constraint infer type parameter remapped reverse mapped contextual type

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Tuple<T> = readonly [T, ...T[]];

declare function bindAll<
  TTarget extends EventTarget,
  TTypes extends Tuple<keyof TTarget & `on${any}`>
>(
  target: TTarget,
  bindings: {
    [K in keyof TTypes]: {
      type: TTypes[K];
      listener: (
        ev: Parameters<Extract<TTarget[TTypes[K]], (...args: any[]) => any>>[0]
      ) => void;
    };
  }
): void;

bindAll({} as HTMLButtonElement, [
  {
    type: "onclick",
    listener: (event) => {
      event
      // ^? actual: string | Event | MouseEvent | ..., expected: MouseEvent
    },
  },
  {
    type: "onkeydown",
    listener: (event) => {
      event
      // ^? actual: string | Event | MouseEvent | ..., expected: KeyboardEvent
    },
  },
]);

πŸ™ Actual behavior

It should be possible to provide per element types using reverse mapped types when mapping over a type parameter with a tuple constraint. It works OK when at the argument position we use [...{ [K in keyof TTypes]: { /*...*/ } }] instead of { [K in keyof TTypes]: { /*...*/ } }. That is a weird workaround though.

πŸ™‚ Expected behavior

I would expect the contextual types to be correctly provided for each element separately, just like in the case of [...{ [K in keyof TTypes]: { /*...*/ } }] (TS playground)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants