【问题标题】:TypeScript: Any techniques to circuit-break a recursive conditional type inference?TypeScript:有什么技术可以中断递归条件类型推断?
【发布时间】:2019-08-05 00:03:50
【问题描述】:

我正在尝试键入一个函数,该函数接受一个将一系列项目减少为一个累积值的参数。这是我所拥有的简化:

// A stub type for the items
interface Item { t: 'item'; }

function paginate<A>(reduce: (acc: A, item: Item, index: number) => A): Promise<A> {
  // ... stub for actual implementation ...
  return true as unknown as Promise<A>;
}

如果我调用这个函数,我不会得到我想要的推理类型。我希望当reduce 的返回类型已知时,第一个参数(acc)也应该被推断为该类型。

// expected: Promise<number>, inferred: Promise<{}>
const result = paginate((acc, item, index) => {
  acc; // expected: number, inferred: {}
  item; // expected: Item, inferred: Item
  index; // expected number, inferred: number

  return 5;
});

我尝试使用带有推理的条件类型来解决这个问题,但是我尝试过的所有变体都失败了,因为它们没有将acc 的泛型类型参数限制为任何特定的东西,或者因为推理是递归的而失败.我不完全确定。

type Reducer<A> = (acc: A, item: Item, index: number) => A;
type Accumulator<Reduce> = Reduce extends (acc: infer A, item: Item, index: number) => infer A ? A : never;

// Probably too loose (using an any)
function paginateA<R extends Reducer<any>>(reduce: R): Promise<Accumulator<R>> {
  // ...
  return true as unknown as Promise<Accumulator<R>>;
}

// expected: Promise<number>, inferred: Promise<number>
const resultA = paginateA((acc, item, index) => {
  acc; // expected: number, inferred: any
  item; // expected: Item, inferred: Item
  index; // expected number, inferred: number

  return 5;
});

// Probably too recursive (tried to circuit-break with only inferring the return type first)
function paginateB<R extends Reducer<ReturnType<R>>>(reduce: R): Promise<Accumulator<R>> {
  // ...
  return true as unknown as Promise<Accumulator<R>>;
}

// expected: Promise<number>, inferred: Promise<any>
const resultB = paginateB((acc, item, index) => {
  acc; // expected: number, inferred: any
  item; // expected: Item, inferred: Item
  index; // expected number, inferred: number

  return 5;
});

是否有任何技术可以“断路”递归条件类型推断?我看到了Anders mention that some class of recursive inference is okay(即使快速信息将类型显示为any),但我无法理解发生这种情况的条件。

我还缺少其他一些技术吗? paginateA() 似乎效果最好,因为至少它得到了resultA 的类型是正确的。有什么原因吗?

这里有一个playground with all the above code 来操作。

【问题讨论】:

  • 嗯,在我的 TS3.4 预览中(使用 typescript@next)我得到了 Promise&lt;number&gt; for result,但我在 TS3.3 及以下看到的是 Promise&lt;{}&gt;。也许只等几个星期?

标签: javascript typescript functional-programming type-inference conditional-types


【解决方案1】:

正如我所说,我认为您尝试实现的 A 的特定推断可能会在 TypeScript 3.4 及更高版本中自动发生,但尚未发布。对于 TypeScript 3.3 及以下版本:


我见过在你想说的这种情况下使用的特殊技术

declare function f<T extends Something<T>>(x: T): void; // circularity error or other issue

是让泛型T不受约束,并通过交集将约束放置在函数参数上:

declare function f<T>(x: T & Something<T>): void; // works now

我找不到这方面的规范文档,但我认为它的工作方式是,当您调用 f(x) 时,编译器会尝试从 T &amp; Something&lt;T&gt; 类型的 x 推断 T。由于xT &amp; Something&lt;T&gt;,因此从交叉路口的工作方式来看,它必须是T。所以x参数的类型被用作T。然后它将检查与Something&lt;T&gt; 的交叉点,如果这不起作用,您会收到编译器错误。

让我们在你的情况下尝试一下,但在我们这样做之前,有一个很大的警告:你可能无法让编译器都从你作为 @987654341 传入的值推断 paginate()R 类型参数@、从对paginate()的调用中推断出的R的类型推断您作为reduce传入的值的参数类型。也就是说,R 将被推断Reducer&lt;number&gt;,但您必须注释 (acc:number, item:Item, index:number)... 必须将R指定Reducer&lt;number&gt;,编译器将推断acc, item, index的类型。你想要两种方式,但我认为编译器不够聪明。或者至少我不能让它发生。所以我现在假设你试图从一个完全注释的reduce 回调中推断出R

interface Item {
  t: "item";
}

type Reducer<A> = (acc: A, item: Item, index: number) => A;
type Accumulator<Reduce> = Reduce extends Reducer<infer A> ? A : never;

declare function paginateC<R extends Reducer<any>>(
  reduce: R & Reducer<ReturnType<R>> // intersection here
): Promise<Accumulator<R>>;

// Promise<number> as desired
const resultC = paginateC((acc: number, item: Item, index: number) => 5);

// error, string is not number:
paginateC((x: number, y: Item, z: number)=>"string"); 

// okay, since string is a subtype of unknown
paginateC((x: unknown, y: Item, z: number)=>"string")

// also works with any
paginateC((x: any, y: Item, z: number)=>"string")

所以没关系。


回到推断acc, item, index的问题...您始终可以像这样指定R

// acc, item, index inferred from specified R
paginateC<Reducer<number>>((acc, item, index) => 5);

但你不想这样做。

事实上,我不希望编译器将acc 参数缩小到您想要的A,因为函数参数总是可以安全地扩展(contravariance of function parameters)。编译器可能会将acc 保留为{}unknownany 等宽泛的名称,除非您对其进行注释或如上所述手动指定R

am有点惊讶,它没有将itemindex 分别缩小到Itemnumber,而不是将它们保留为any。但是可能没有太多工作要做,因为它是类型推断的known limitation,它不会在多次传递中发生。哦,好吧。


好的,希望对您有所帮助。祝你好运!

【讨论】:

  • 感谢您对类型缩小和各种权衡的彻底探索!听起来您指的是 TypeScript 3.4 中的一些特定更改,您介意指出您认为哪些更改可能会以我一直在寻找的方式进行推理吗?
  • 我指的是我使用 TypeScript 3.4-dev 时编译器的行为,所以我不知道具体是什么变化导致它开始正确推断。查看 3.3 之后合并的拉取请求,我发现 this one 与泛型和上下文类型的推断有关,所以它可能就是那个。但这只是猜测。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 2018-10-20
  • 1970-01-01
  • 2022-01-07
  • 2023-01-04
  • 2018-07-03
  • 1970-01-01
相关资源
最近更新 更多