diff --git a/types/util.promisify/auto.d.ts b/types/util.promisify/auto.d.ts new file mode 100644 index 0000000000..111dd3a6c9 --- /dev/null +++ b/types/util.promisify/auto.d.ts @@ -0,0 +1,3 @@ +import shim = require("./shim"); + +export {}; diff --git a/types/util.promisify/index.d.ts b/types/util.promisify/index.d.ts index f3ccaf217c..dee4bb30a0 100644 --- a/types/util.promisify/index.d.ts +++ b/types/util.promisify/index.d.ts @@ -1,36 +1,41 @@ // Type definitions for util.promisify 1.0 // Project: https://github.com/ljharb/util.promisify#readme // Definitions by: Adam Voss +// Piotr Roszatycki // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/*~ Note that ES6 modules cannot directly export callable functions. - *~ This file should be imported using the CommonJS-style: - *~ import x = require('util.promisify'); - *~ - *~ Refer to the documentation to understand common - *~ workarounds for this limitation of ES6 modules. - */ -export = promisify; +/// -declare function promisify(f: (...args: any[]) => void): (...args: any[]) => Promise; +// tslint:disable:ban-types +interface CustomPromisify extends Function { + __promisify__: TCustom; +} + +declare function promisify(fn: CustomPromisify): TCustom; +declare function promisify(fn: (callback: (err: Error | null, result: TResult) => void) => void): () => Promise; +declare function promisify(fn: (callback: (err?: Error | null) => void) => void): () => Promise; +declare function promisify(fn: (arg1: T1, callback: (err: Error | null, result: TResult) => void) => void): (arg1: T1) => Promise; +declare function promisify(fn: (arg1: T1, callback: (err?: Error | null) => void) => void): (arg1: T1) => Promise; +declare function promisify(fn: (arg1: T1, arg2: T2, callback: (err: Error | null, result: TResult) => void) => void): (arg1: T1, arg2: T2) => Promise; +declare function promisify(fn: (arg1: T1, arg2: T2, callback: (err?: Error | null) => void) => void): (arg1: T1, arg2: T2) => Promise; +declare function promisify(fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: Error | null, result: TResult) => void) => void): (arg1: T1, arg2: T2, arg3: T3) => Promise; +declare function promisify(fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: Error | null) => void) => void): (arg1: T1, arg2: T2, arg3: T3) => Promise; +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err: Error | null, result: TResult) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise; +declare function promisify(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err?: Error | null) => void) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise; +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: (err: Error | null, result: TResult) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise; +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: (err?: Error | null) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise; +declare function promisify(fn: Function): Function; declare namespace promisify { - interface implementation { - (fn: (...args: any[]) => void): (...args: any[]) => Promise; - custom: symbol; - customPromisifyArgs: symbol | undefined; - } - - const custom: symbol; - const customPromisifyArgs: symbol; - function getPolyfill(): implementation; - const implementation: implementation; - function shim(): implementation; + function getPolyfill(): typeof promisify; + const implementation: typeof promisify; + function shim(): typeof promisify; } -declare module "util" { - let promisify: { - (fn: (...args: any[]) => void): (...args: any[]) => Promise; - custom: symbol; - }; -} +export = promisify; diff --git a/types/util.promisify/polyfill.d.ts b/types/util.promisify/polyfill.d.ts new file mode 100644 index 0000000000..5056d4e9f4 --- /dev/null +++ b/types/util.promisify/polyfill.d.ts @@ -0,0 +1,29 @@ +/// + +// tslint:disable:ban-types max-line-length +interface CustomPromisify extends Function { + __promisify__: TCustom; +} + +declare function getPolyfill(): (fn: CustomPromisify) => TCustom; +declare function getPolyfill(): (fn: (callback: (err: Error | null, result: TResult) => void) => void) => () => Promise; +declare function getPolyfill(): (fn: (callback: (err?: Error | null) => void) => void) => () => Promise; +declare function getPolyfill(): (fn: (arg1: T1, callback: (err: Error | null, result: TResult) => void) => void) => (arg1: T1) => Promise; +declare function getPolyfill(): (fn: (arg1: T1, callback: (err?: Error | null) => void) => void) => (arg1: T1) => Promise; +declare function getPolyfill(): (fn: (arg1: T1, arg2: T2, callback: (err: Error | null, result: TResult) => void) => void) => (arg1: T1, arg2: T2) => Promise; +declare function getPolyfill(): (fn: (arg1: T1, arg2: T2, callback: (err?: Error | null) => void) => void) => (arg1: T1, arg2: T2) => Promise; +declare function getPolyfill(): (fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: Error | null, result: TResult) => void) => void) => (arg1: T1, arg2: T2, arg3: T3) => Promise; +declare function getPolyfill(): (fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: Error | null) => void) => void) => (arg1: T1, arg2: T2, arg3: T3) => Promise; +declare function getPolyfill(): ( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err: Error | null, result: TResult) => void) => void, +) => (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise; +declare function getPolyfill(): (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err?: Error | null) => void) => void) => (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise; +declare function getPolyfill(): ( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: (err: Error | null, result: TResult) => void) => void, +) => (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise; +declare function getPolyfill(): ( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: (err?: Error | null) => void) => void, +) => (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise; +declare function getPolyfill(): (fn: Function) => Function; + +export = getPolyfill; diff --git a/types/util.promisify/shim.d.ts b/types/util.promisify/shim.d.ts new file mode 100644 index 0000000000..316dc5e996 --- /dev/null +++ b/types/util.promisify/shim.d.ts @@ -0,0 +1,3 @@ +import getPolyfill = require("./polyfill"); + +export = getPolyfill; diff --git a/types/util.promisify/tsconfig.json b/types/util.promisify/tsconfig.json index 041c10a94c..921e18f779 100644 --- a/types/util.promisify/tsconfig.json +++ b/types/util.promisify/tsconfig.json @@ -17,7 +17,10 @@ "forceConsistentCasingInFileNames": true }, "files": [ + "auto.d.ts", "index.d.ts", + "polyfill.d.ts", + "shim.d.ts", "util.promisify-tests.ts" ] } \ No newline at end of file diff --git a/types/util.promisify/util.promisify-tests.ts b/types/util.promisify/util.promisify-tests.ts index 9aa058ee69..50573765bb 100644 --- a/types/util.promisify/util.promisify-tests.ts +++ b/types/util.promisify/util.promisify-tests.ts @@ -1,11 +1,15 @@ -/// - import * as util from 'util'; -import * as fs from "fs"; +import * as fs from 'fs'; import promisify = require('util.promisify'); -// tslint:disable-next-line ban-types -let readFile: Function = promisify(fs.readFile); +import 'util.promisify/auto'; -promisify.shim(); +let readFile = promisify(fs.readFile); readFile = util.promisify(fs.readFile); + +let implementation = promisify.getPolyfill(); +implementation = promisify.shim(); +implementation = promisify.implementation; + +import promisifyShim = require('util.promisify/shim'); +promisifyShim();