如何指定我想在Typescript中重载的函数?

我试图promisify标准节点fs.writeFile。 我有节点和蓝鸟这是我的诺言在这里selectlib的typings:

const f = Promise.promisify(fs.writeFile) return f(file, content); // Should be a promise 

这不起作用,因为:

提供的参数不匹配调用目标的任何签名。 const f:(arg1:string)=> Promise <{}>

所以我select错误的重载方法,因为promisify的调用不能真正知道我猜。 或者甚至不是这样,而是可选参数的东西,因为这些是三个重载的writeFile:

 export function writeFile(filename: string, data: any, callback?: (err: NodeJS.ErrnoException) => void): void; export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: number; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void; export function writeFile(filename: string, data: any, options: { encoding?: string; mode?: string; flag?: string; }, callback?: (err: NodeJS.ErrnoException) => void): void; 

promisify有很多重载的参数#看起来像这样:

 /** * Returns a function that will wrap the given `nodeFunction`. Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument. * * If the `nodeFunction` calls its callback with multiple success values, the fulfillment value will be an array of them. * * If you pass a `receiver`, the `nodeFunction` will be called as a method on the `receiver`. */ static promisify<T>(func: (callback: (err:any, result: T) => void) => void, receiver?: any): () => Promise<T>; static promisify<T, A1>(func: (arg1: A1, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1) => Promise<T>; static promisify<T, A1, A2>(func: (arg1: A1, arg2: A2, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2) => Promise<T>; static promisify<T, A1, A2, A3>(func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3) => Promise<T>; static promisify<T, A1, A2, A3, A4>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Promise<T>; static promisify<T, A1, A2, A3, A4, A5>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, result: T) => void) => void, receiver?: any): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Promise<T>; static promisify(nodeFunction: Function, receiver?: any): Function; 

所以这似乎是错误的promisify正在使用。 有没有更清洁的方式来实现我想要的? 目前我必须解决这个问题:

 const writeFile : (filename: string, data: any, callback: (err: NodeJS.ErrnoException) => void) => void = fs.writeFile const f = Promise.promisify(writeFile) return f(file, content); 

我觉得这是非常丑陋和冗长的…

我还没有能够build立一个很好的简单工作的例子,但下面的types断言将允许你暗示和你所期望的:

 const f = <(file: string, content: string) => void> Promise.promisify(fs.writeFile) 

您可以修改<(file: string, content: string) => void>以更好地描述您期望可用的内容,或者使用更广泛的types<Function>或与<any>一起变为dynamic。

第一个选项是多一点工作,但是当你稍后调用f (在这种情况下是void ,但你可以想象这种返回types将被更有效地使用的情况)时,将允许推断返回types。

根据你的评论,我猜你会需要…

 const f = <(file: string, content: string) => Promise<any>> Promise.promisify(fs.writeFile) 

如果你知道两个可能的结果,也就是说Promise<any>可能是:

 Promise<ErrnoException | MySuccessfulType> 

这将允许Promise<ErrnoException>的返回types或Promise<ErrnoException>的返回types。