TypeScript在类型构造函数中推断回调返回类型

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

我想为一个函数编写一个类型构造函数,它接收一个类型S和一个从S到另一个类型的函数,然后在S上应用该函数并返回结果:

// This works but it's tied to the implementation
function dig<S, R>(s: S, fn: (s: S) => R): R {
  return fn(s);
}

// This works as separate type constructor but I have to specify `R`
type Dig<S, R> = (s: S, fn: (s: S) => R) => R;

// Generic type 'Dig' requires 2 type argument(s).
const d: Dig<string> = (s, fn) => fn(s); 

那么如何编写一个Dig<S>类型的构造函数来推断传递的fn参数的返回类型而不指定R

typescript generics type-inference
1个回答
3
投票

从TS3.4开始,不支持partial type argument inference,所以你不能轻易让编译器让你指定S但推断R。但是从你的例子来看,你看起来并不想将R推断为某种具体类型,但是允许它保持通用,这样当你调用fn时,d()的返回类型可以是它想要的任何东西。

所以看起来你真的想要这种类型:

type Dig<S> = <R>(s: S, fn: (s: S) => R) => R;

这是一种“双重通用”类型,从某种意义上说,一旦你指定S,你仍然有一个依赖于R的泛型函数。这适用于您给出的示例:

const d: Dig<string> = (s, fn) => fn(s);

const num = d("hey", (x) => x.length); // num is inferred as number
const bool = d("you", (x) => x.indexOf("z") >= 0); // bool inferred as boolean

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

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