我正在寻找一种方法来获取传递给构造函数的函数参数的类型信息。
export class Test<T> {
constructor(
test: ((... params : any[]) => T) | (( ... params : any[] ) => Promise<T>
) {
// Convert function or promise to promise
this.test = this.test = (...params: any[]) => Promise.resolve(test(...params))
}
// How I store the promisify'd function/promise
private test : (...params) => Promise<T>
// I want to see typing information on the parameters for this method
public async execute(...params: any[]) : Promise<any> {
try {
return await this.test(...params)
} catch (error) {
return error
}
}
当传入函数或承诺时,我将其存储为承诺。目前打字信息丢失。
在execute方法中,我希望能够看到要传入的参数的类型信息;它们应该匹配原始函数的参数。
例如;
let test = new Test( (a: number, b: string) : string => `${a}${b}`)
let output : string = test.execute(1, 'b') // should compile
let output : number = test.execute(1, 'b') // should not compile
let output : string = test.execute('b', 1) // should not compile, and this is the one I'm looking for.
有任何想法吗?我想知道我是否可以keyof
巧妙地使用。
已提议输入可变参数参数,但仍在讨论中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句