TypeScript:电路中断递归条件类型推断的任何技术?

问题描述 投票:4回答:1

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

// 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

javascript typescript functional-programming type-inference conditional-types
1个回答
2
投票

正如我所说的,我认为你试图实现的AReducer<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类型的x推断出T & Something<T>。由于xT & Something<T>,它必须是T,从交叉路口的工作方式。所以x参数的类型用作T。然后它将检查与Something<T>的interesection,如果这不起作用,你会得到一个编译器错误。

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

reduce

所以没关系。


通过推断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{}unknown这样宽泛的东西,除非你注释它或如上所述手动指定any

我有点意外的是,它没有将Ritem分别缩小到indexItem而不是将它们留作number。但是可能没什么可做的,因为它是类型推断的any,它不会在多次传递中发生。那好吧。


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

© www.soinside.com 2019 - 2024. All rights reserved.