【发布时间】:2017-11-09 17:56:14
【问题描述】:
我正在尝试编写一个高阶函数来包装输入函数并缓存最近调用的结果作为副作用。基本函数 (withCache) 如下所示:
function cache(key: string, value: any) {
//Some caching logic goes here
}
function withCache<R>(key: string, fn: (...args: any[]) => R): (...args: any[]) => R {
return (...args) => {
const res = fn(...args);
cache(key, res);
return res;
}
}
const foo = (x: number, y: number) => x + y;
const fooWithCache = withCache("foo", foo);
let fooResult1 = fooWithCache(1, 2); // allowed :)
let fooResult2 = fooWithCache(1, 2, 3, 4, 5, 6) // also allowed :(
现在我知道我可以使用函数重载使这种类型安全 - 在一定程度上 - 使用函数重载,如下所示:
function withCache<R>(key: string, fn: () => R): () => R
function withCache<R, T1>(key: string, fn: (a: T1) => R): (a: T1) => R
function withCache<R, T1, T2>(key: string, fn: (a: T1, b: T2) => R): (a: T1, b: T2) => R
function withCache<R>(key: string, fn: (...args: any[]) => R): (...args: any[]) => R {
// implementation ...
}
const foo = (x: number, y: number) => x + y;
const fooWithCache = withCache("foo", foo);
let fooResult1 = fooWithCache(1, 2); // allowed :)
let fooResult2 = fooWithCache(1, 2, 3, 4, 5, 6) // not allowed :)
当我尝试允许带有可选参数的函数时问题就来了(最后一个重载是新的):
function withCache<R>(key: string, fn: () => R): () => R
function withCache<R, T1>(key: string, fn: (a: T1) => R): (a: T1) => R
function withCache<R, T1, T2>(key: string, fn: (a: T1, b: T2) => R): (a: T1, b: T2) => R
function withCache<R, T1, T2>(key: string, fn: (a: T1, b?: T2) => R): (a: T1, b?: T2) => R
function withCache<R>(key: string, fn: (...args: any[]) => R): (...args: any[]) => R {
// implementation ...
}
const foo = (x: number, y?: number) => x + (y || 0);
const fooWithCache = withCache("foo", foo);
let fooResult1 = fooWithCache(1); // allowed :)
let fooResult2 = fooWithCache(1, 2) // not allowed, but should be :(
问题似乎是 Typescript 为withCache 选择了错误的重载,结果是fooWithCache 的签名是(a: number) => number。我希望fooWithCache 的签名是(a: number, b?: number) => number,就像foo。
有没有办法解决这个问题?
(顺便说一句,有没有办法声明重载,这样我就不必重复每个重载的函数类型(...) => R?)
编辑:
想出了关于不重复函数类型的第二个问题:只需定义它!
type Function1<T1, R> = (a: T1) => R;
// ...
function withCache<T1, R>(fn: Function1<T1, R>): Function1<T1, R>;
编辑:
这对于异步函数如何工作(假设您想要缓存结果而不是 Promise 本身)?你当然可以这样做:
function withCache<F extends Function>(fn: F) {
return (key: string) =>
((...args) =>
//Wrap in a Promise so we can handle sync or async
Promise.resolve(fn(...args)).then(res => { cache(key, res); return res; })
) as any as F; //Really want F or (...args) => Promise<returntypeof F>
}
但是与同步函数一起使用是不安全的:
//Async function
const bar = (x: number) => Promise.resolve({ x });
let barRes = withCache(bar)("bar")(1).x; //Not allowed :)
//Sync function
const foo = (x: number) => ({ x });
let fooRes = withCache(foo)("bar")(1).x; //Allowed, because TS thinks fooRes is an object :(
有没有办法防止这种情况发生?或者编写一个对两者都安全有效的函数?
总结:@jcalz 的回答是正确的。在可以假定同步函数的情况下,或者可以直接使用 Promises 而不是它们解析的值的情况下,断言函数类型可能是安全的。但是,如果没有unimplementedlanguageimprovements,上述同步或异步场景是不可能的。
【问题讨论】:
-
根据您的代码,这应该是
let fooResult2 = fooWithCache(1, 2)的正确过载? -
我希望
fooWithCache的签名是(a: number, b?: number) => number,就像foo。 -
编辑了问题以澄清我的期望。
-
当你使用
const foo = (x: number, y?: number) => x + (y || 0);时console.log(fooWithCache)的值是多少你试过用const foo = (x: number, y: number = 0) => x + y ;为第二个参数设置一个默认值。
标签: typescript generics higher-order-functions