Skip to content

Function argument is not infered correctlyΒ #62336

@APZelos

Description

@APZelos

πŸ”Ž Search Terms

function, argument, parameter, generic, callback, infer

πŸ•— Version & Regression Information

By trying out different versions in Playground, it looks like it works correctly in version 4.7.4 and lower

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/CYUwxgNghgTiAEAzArgOzAFwJYHtXzCgggB4AVAPgApFUAueKqBgZwxi1QHMAaeAIwZkAlPAC8FeGgDWqHAHdUwhlUHwR4yTLmKA3ACh9oSLAQp02PElQkAgjC4t4IAB4YQqYE6ioAngG0AXWp9eGsVADoo2EcGe0dRCSlUWQVUHn1lRiiImJY4hxZErRSdVAN9MDw2eDgWZAgMcQIiCBpUGjRMXHwmPjVUZABbfhAYUQBvAF9hYV0gA

πŸ’» Code

declare function call<T>(fn: (a: string, b: T) => unknown): (b: T) => unknown;

declare function fn<Args extends any[]>(
  fn: (...args: Args) => unknown,
): (...args: Args) => unknown;

const result = call(fn(function (a, b: number) {}));

πŸ™ Actual behavior

The a parameter's type is any.

πŸ™‚ Expected behavior

The a paramater's type should be string

Additional information about the issue

I encountered this while trying to create a helper function in a codebase that uses Effect:

import { Effect } from "effect"

function call<A, E, R, T = void>(fn: (a: string, b: T) => Effect.Effect<A, E, R>) {
  return (b: T) => fn("value", b)
}

const fnA = call((a, b: number) => // a is `string`
  Effect.gen(function* () {
    return yield* Effect.succeed(`${a}: ${b}`)
  }),
)

const fnB = call(
  Effect.fn(function* (a, b: number) { // a is `any`?
    return yield* Effect.succeed(`${a}: ${b}`)
  }),
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Help WantedYou can do thisPossible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions