我想为一个函数编写一个类型构造函数,它接收一个类型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
?
从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
好的,希望有所帮助。祝好运!