microsoft / microsoft/TypeScript
Function argument is not infered correctly
未关闭
还没有人认领这个 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
💻 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}`)
}),
)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行链接的 TypeScript Playground 示例,并比较直接回调和由 fn 包装的回调中 a 的推断类型。调查 call 和 fn 周围的泛型回调参数推断;当包装后的回调将 a 推断为 string 而不是 any,同时保留现有的 b: number 推断时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- typescript
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100