2017-06-14 53 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>;