我正在尝试编写一个高阶函数来包装输入函数并缓存最近调用的结果作为副作用。基本功能(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
is (a: number) => number
。我希望fooWithCache
的签名为(a: number, b?: number) => number
, 就像foo
。
有没有什么办法解决这一问题?
(As a side note, is there any way to declare the overloads so I don't have to repeat each overload's function type (...) => R
?)
Edit:
解决了我关于不重复函数类型的第二个问题:只需定义它!
type Function1<T1, R> = (a: T1) => R;
// ...
function withCache<T1, R>(fn: Function1<T1, R>): Function1<T1, R>;
Edit:
对于异步函数来说,这将如何工作(假设您想缓存结果而不是 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 的答案是正确的。在可以假定同步函数的情况下,或者可以直接使用 Promise 而不是它们解析的值的情况下,断言函数类型可能是安全的。然而,如果没有一些现有技术,上述同步或异步场景是不可能实现的。未实现的 language 改进.