Skip to content

NoInfer and overloaded function argument inferenceΒ #57873

Closed as not planned
Closed as not planned
@kevinresol

Description

@kevinresol

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions