2017-06-14 72 views
0

从v8.0.0节点提供util.promisify() API。现在我试图将一些回调式方法转换为异步/等待样式。 在打字稿,util.promisify()可能无法继承方法的签名:有没有任何方法继承方法签名util.promisify打字稿?

import fs = require('fs'); 

export namespace AsyncFs { 
    export const lstat = util.promisify(fs.lstat); 
    // there's no method signature, only shows as "Function" 
} 

虽然我们可以为每个方法添加新的签名......

export const lstat = util.promisify(fs.lstat) as (path: string | Buffer) => fs.Stats; 

所以我在寻找自动继承签名的好方法。可能吗?你有什么好主意吗?

谢谢。

+0

“inherit”,你的意思是*推断*? – Bergi

回答

1

如果不是由TS内部处理,那么您可能必须自己定义util.promisify()的类型,您自己的做法与what they do for Bluebird's promisify() static function in DefinitelyTyped类似。

static promisify<T>(func: (callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions):() => Bluebird<T>; 
    static promisify<T, A1>(func: (arg1: A1, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1) => Bluebird<T>; 
    static promisify<T, A1, A2>(func: (arg1: A1, arg2: A2, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2) => Bluebird<T>; 
    static promisify<T, A1, A2, A3>(func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2, arg3: A3) => Bluebird<T>; 
    static promisify<T, A1, A2, A3, A4>(func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result: T) => void) => void, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Bluebird<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, options?: Bluebird.PromisifyOptions): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Bluebird<T>;