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

NoInfer and overloaded function argument inference #57873

Closed
kevinresol opened this issue Mar 21, 2024 · 3 comments
Closed

NoInfer and overloaded function argument inference #57873

kevinresol opened this issue Mar 21, 2024 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@kevinresol
Copy link

kevinresol commented Mar 21, 2024

πŸ”Ž Search Terms

NoInfer, overload

πŸ•— Version & Regression Information

Version 5.4.3

  • I was unable to test this on prior versions because this is a new feature

⏯ Playground Link

https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXwAcYdhkwQAKRALngDkcBJVREGAHgoEp4BeAPngA3HFmD8utbn0EixAbgBQoSLAQp02PIWKly7AIL8qtBs1YcKsAOa0DPAcNHjJ8KzFvx7MpwsWKwPABnDHgADykHWWc+HRIySmlHAEYuJUDUEPgATykbWhCYLFRrKN9gWKJ48goKITLUpSA

πŸ’» Code

declare function produce(f: NoInfer<() => void>): () => void;
declare function produce<A>(f: NoInfer<(arg: A) => void>): (arg: A) => void;

const x: () => void = produce(() => 1); // works
const y: (arg: string) => void = produce((v) => 1); // Parameter 'v' implicitly has an 'any' type.

πŸ™ Actual behavior

Because const y is explicitly typed and thanks to the new utility NoInfer type, the compiler is able to figure out (from the return type) it is calling the produce<string>() overload. (Without NoInfer it would just be produce<any>()).

However, apparently it does not apply this knowledge to the function parameter f and thus the non type-hinted argument v is inferred as any

Screenshot 2024-03-21 at 12 04 31

πŸ™‚ Expected behavior

Since the compiler already knows f is of type NoInfer<(arg:string)=>void> it should be able to infer the lambda argument v to be of string type.

Screenshot 2024-03-21 at 12 13 46

Additional information about the issue

No response

@Andarist
Copy link
Contributor

This isn't related to NoInfer as this doesn't assign the correct contextual parameter here either:

declare function produce(f: () => void): () => void;
declare function produce<A>(f: (arg: A) => void): (arg: A) => void;
const y: (arg: string) => void = produce((v) => 1);

There is a good chance that this PR might solve your issue: #57421

@Andarist
Copy link
Contributor

Since @RyanCavanaugh has prepared a playground for that mentioned PR (thanks!) we can now quickly confirm that it fixes this issue: TS playground

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 22, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants