microsoft / microsoft/TypeScript

Function argument is not infered correctly

Open
#62,336 1 comment 0 reactions 0 assignees View on GitHub
Domain: check: Type Inference Help Wanted Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.3k
PR merge metrics
PR metrics pending

Description

### 🔎 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

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

declare function fn(
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:

```ts
import { Effect } from "effect"

function call(fn: (a: string, b: T) => Effect.Effect) {
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}`)
}),
)
```

Contributor guide

Open the contributing guide

Research direction

Start by running the linked TypeScript Playground example and compare the inferred types of `a` in the direct callback and the `fn`-wrapped callback. Investigate generic callback argument inference around `call` and `fn`; done means the wrapped callback infers `a` as `string` rather than `any`, while preserving the existing `b: number` inference.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.