【发布时间】:2017-03-26 15:51:00
【问题描述】:
我正在尝试在 typescript 中编写一个小型缓存包装器(简化的伪演示代码):
const cache = {};
export function cachify<T, V>(name:string, getFunction: (i:V)=>Promise<T>): (i:V) => Promise<T> {
return function() {
return cache[name] || getFunction.apply(this,arguments)
}
})
如果我的函数只有一个参数,例如
function isNameFancy(name:string) {
return Promise.resolve(true)
}
const isNameFancyWithCache = cachify(isNameFancy)
但是,正如我指定的 i:V 这仅对一个参数有效。
如果我有第二个功能,例如isPersonFancy 不行:
function isPersonFancy(personAge: number, personName: string) {
return Promise.resolve(true)
}
const isPersonFancyWithCache = cachify(isPersonFancy)
如何更改我的 cachify 函数类型以使其适用于两种情况?
【问题讨论】:
标签: javascript typescript typescript-typings strong-typing