【发布时间】:2021-05-03 14:56:13
【问题描述】:
我想创建一个包装任意函数的函数,但对参数和结果保持相同的类型。例如,这将包装一个函数 - 但类型将变为 any:
// Note that the returned wrappedFn takes the same arguments as fn, and returns
// a value of the same type as the one returned by fn.
function myWrapper(fn: (...args: any[]) => any): (...args: any[]) => any {
return function wrappedFn(...args: any[]) {
// ...some arbitrary code goes here
return fn(...args);
};
}
function repeatString(str: string, times: number): string {
return Array(times + 1).join(str);
}
// TS is aware that wrappedRepeatString is a function, but it infers that
// it takes any parameters, rather than a string and a number.
const wrappedRepeatString = myWrapper(repeatString);
// The result of this call is "foofoofoo" (a string), but TS sees it as "any".
const repeatedString = wrappedRepeatString("foo", 3);
// Since repeatedString is of type "any", this compiles, even though
// it'd crash as there's no "toFixed" method on strings.
repeatedString.toFixed();
我可以输入 myWrapper 来获取一个函数,该函数接受一个字符串和一个数字作为参数,并返回一个数字 - 但如果我想将它与具有不同参数或返回类型的函数一起使用,我需要再写一个。理想情况下,我可以编写一个包装器,使 TS 能够推断出wrappedFn(返回的函数)的参数和返回值与`fn 的相同。
请注意,仅将其余参数输入为数组类型(例如 ...args: string[])是行不通的,因为参数也可能属于不同的类型。
【问题讨论】:
-
对不起,我不明白这个问题。您能否提供没有错误的代码示例?
-
我的错。我已经修改了代码并对问题给出了更清晰的描述。 (我希望。)
标签: typescript typescript-generics