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

Annotated callback parameters are not prioritized above un-annotated ones #59548

Closed
Rugvip opened this issue Aug 7, 2024 · 2 comments
Closed

Comments

@Rugvip
Copy link

Rugvip commented Aug 7, 2024

πŸ”Ž Search Terms

"callback inference", "method inference"

πŸ•— Version & Regression Information

Testing this in the playground it seems to be an issue all the way back to 3.3.3

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.5.4#code/GYVwdgxgLglg9mABFApgZygHgCoD4AUcADrAmgFyIDeAUIosGAIyX5SXYCUiAvLogDc4MACYBuOgzAAmVu0Rde-IaIkBfTpRUjqamjQD0BxADFw0eEiIAnYmkQBaRAHc41gNZoaqDPlr1GFmQlXQAaSUZZRDZKDGsYMABzbj4wmg0JQ2MAWRQoAAs4EXtnGALEAEMwMDgoCtIkYBhrDEcXN09vdCg-COYYxDiE5LSAmTZuKjVwjP0jRFyCopKy-Mrq2vrLRAAbCtanETh0MAByKHaPLt9-KSYJ0alpAaGkyen0zjEgA

πŸ’» Code

function test<T>(options: {
  fn1: (t: T) => void;
  fn2: (t: T) => void;
}): void {}

// Function props - works
test({
  fn1: t => {},
  fn2: (t: string) => {},
});

// Methods with annotation first - works
test({
  fn1(t: string) {},
  fn2(t) {},
});

// Methods with annotation last - doesn't work
test({
  fn1(t) {},
  fn2(t: string) {},
});

πŸ™ Actual behavior

When providing multiple function properties with the same type parameter as a function parameter, the type parameter will always be inferred from the first function property.

πŸ™‚ Expected behavior

I'm expecting the explicitly annotated function callbacks to have higher inference priority than the ones without annotations.

Additional information about the issue

Not sure whether this is a bug or intended behavior, but though I'd report it to check. It seems related to #18654, although turning off strict function types doesn't change the behavior, it just removes the type error where the unknown param is not assignable to string.

It also looks like it's related to for example #58746, but slightly different.

@Andarist
Copy link
Contributor

Andarist commented Aug 7, 2024

This one boils down to #47599

Contextual parameters are assigned in source order and it isn't a unique limitation for callbacks contained in objects. The same happens for arguments lists: TS playground

@Rugvip
Copy link
Author

Rugvip commented Aug 7, 2024

@Andarist yep I noticed that too, just found the object form to be a simpler thing to report really πŸ˜…

Closing this and adding a πŸ‘ to #47599 instead, thank you! πŸ™

@Rugvip Rugvip closed this as completed Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants