potential bug related to generics

未关闭
#55,173 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

从链接的 TypeScript Playground 开始,围绕 ValidCompose、ValidPipe 和两个 pipe 调用精简泛型 pipe 示例。调查泛型函数 f 与 h 的类型检查行为;完成标准是两个调用都能在没有错误的情况下通过类型检查,同时保留报告中的泛型约束。

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

描述

Needs Investigation

Bug Report

🔎 Search Terms

generics
pipe
functional programming

🕗 Version & Regression Information

5.2.0-dev

  • This is the behavior in every version I tried
⏯ Playground Link

Playground link with relevant code

💻 Code
export type Func = (..._: any[]) => unknown;

export type Unary<Input, Output> = (_: Input) => Output;

export type Tail<L extends unknown[]> = L extends
  readonly [unknown, ...infer LTail] ? LTail
  : L;

type UnaryFn<A, R> = (a: A) => R;
type Arg<F> = F extends UnaryFn<infer A, unknown> ? A : never;
type Res<F> = F extends UnaryFn<any, infer R> ? R : never;

type ValidCompose<F1, F2> = Res<F1> extends (Arg<F2> | Promise<Arg<F2>>) ? F1
  : (arg: Arg<F1>) => Arg<F2>;

type ValidPipe<FS> = FS extends [infer F1, infer F2, ...infer Rest]
  ? [ValidCompose<F1, F2>, ...ValidPipe<[F2, ...Rest]>]
  : FS;

export const pipe = <Fs extends Func[]>(  ...fs: ValidPipe<Fs>) => {}

(<T, Fn extends (x: T) => number>(f: Fn) => {
  const g = (x: T) => x;
  const h = (_: T) => 1;
  pipe(g, f);  // This gives a typing error.
  pipe(g, h);  // This works fine.
});
🙁 Actual behavior

The first pipe invocation results in a typing error.

🙂 Expected behavior

None should have an error, because the generic typing ensures everything is in order.

主要语言
Go
星标
111k
派生
14.4k
平均合并
1 天 15 小时
30 天内合并 PR
106

贡献指南

打开贡献指南

从这里开始

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

microsoft/TypeScript 的其他 Issue

查看 microsoft/TypeScript 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

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