microsoft / microsoft/TypeScript

Function argument is not infered correctly

未关闭
#62,336 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Domain: check: Type Inference Help Wanted Possible Improvement
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

🔎 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}`)
  }),
)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先运行链接的 TypeScript Playground 示例,并比较直接回调和由 fn 包装的回调中 a 的推断类型。调查 callfn 周围的泛型回调参数推断;当包装后的回调将 a 推断为 string 而不是 any,同时保留现有的 b: number 推断时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。