diff --git a/types/util.promisify/auto.d.ts b/types/util.promisify/auto.d.ts index 111dd3a6c9..a09dcc48f3 100644 --- a/types/util.promisify/auto.d.ts +++ b/types/util.promisify/auto.d.ts @@ -1,3 +1,13 @@ -import shim = require("./shim"); +import polyfill = require('./index'); -export {}; +declare module 'util' { + namespace promisify { + /** + * @deprecated + * Not exposed by native `util.promisify` or supported by browserify's `util.promisify`. + * + * Use `util.promisify.custom` instead. + */ + const customPromisifyArgs: typeof polyfill.customPromisifyArgs | undefined; + } +} diff --git a/types/util.promisify/implementation.d.ts b/types/util.promisify/implementation.d.ts new file mode 100644 index 0000000000..4a1d295ca6 --- /dev/null +++ b/types/util.promisify/implementation.d.ts @@ -0,0 +1,59 @@ +import util = require('util'); + +// tslint:disable-next-line: ban-types +declare function promisify(fn: util.CustomPromisify): TCustom; + +declare function promisify( + fn: (callback: (err: any, result: TResult) => void) => void, +): () => Promise; +declare function promisify(fn: (callback: (err?: any) => void) => void): () => Promise; + +declare function promisify( + fn: (arg1: T1, callback: (err: any, result: TResult) => void) => void, +): (arg1: T1) => Promise; +declare function promisify(fn: (arg1: T1, callback: (err?: any) => void) => void): (arg1: T1) => Promise; + +declare function promisify( + fn: (arg1: T1, arg2: T2, callback: (err: any, result: TResult) => void) => void, +): (arg1: T1, arg2: T2) => Promise; +declare function promisify( + fn: (arg1: T1, arg2: T2, callback: (err?: any) => void) => void, +): (arg1: T1, arg2: T2) => Promise; + +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: any, result: TResult) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3) => Promise; +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: any) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3) => Promise; + +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err: any, 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?: any) => 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: any, 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?: any) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise; + +// tslint:disable-next-line: ban-types +declare function promisify(fn: Function): Function; + +declare namespace promisify { + const custom: typeof util.promisify.custom; + + /** + * @deprecated + * Not exposed by native `util.promisify` or supported by browserify's `util.promisify`. + * + * Use `util.promisify.custom` instead. + */ + const customPromisifyArgs: unique symbol; +} + +export = promisify; diff --git a/types/util.promisify/index.d.ts b/types/util.promisify/index.d.ts index dee4bb30a0..64d4877486 100644 --- a/types/util.promisify/index.d.ts +++ b/types/util.promisify/index.d.ts @@ -2,40 +2,75 @@ // Project: https://github.com/ljharb/util.promisify#readme // Definitions by: Adam Voss // Piotr Roszatycki +// ExE Boss // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// -// tslint:disable:ban-types -interface CustomPromisify extends Function { - __promisify__: TCustom; -} +import util = require('util'); + +import polyfill = require('./implementation'); +import getUtilPromisify = require('./polyfill'); +import shimUtilPromisify = require('./shim'); + +// tslint:disable-next-line: ban-types +declare function promisify(fn: util.CustomPromisify): TCustom; + +declare function promisify( + fn: (callback: (err: any, result: TResult) => void) => void, +): () => Promise; +declare function promisify(fn: (callback: (err?: any) => void) => void): () => Promise; + +declare function promisify( + fn: (arg1: T1, callback: (err: any, result: TResult) => void) => void, +): (arg1: T1) => Promise; +declare function promisify(fn: (arg1: T1, callback: (err?: any) => void) => void): (arg1: T1) => Promise; + +declare function promisify( + fn: (arg1: T1, arg2: T2, callback: (err: any, result: TResult) => void) => void, +): (arg1: T1, arg2: T2) => Promise; +declare function promisify( + fn: (arg1: T1, arg2: T2, callback: (err?: any) => void) => void, +): (arg1: T1, arg2: T2) => Promise; + +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: any, result: TResult) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3) => Promise; +declare function promisify( + fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: any) => void) => void, +): (arg1: T1, arg2: T2, arg3: T3) => Promise; -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, + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err: any, 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, callback: (err?: any) => 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, + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: (err: any, 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, + fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, callback: (err?: any) => void) => void, ): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise; + +// tslint:disable-next-line: ban-types declare function promisify(fn: Function): Function; declare namespace promisify { - function getPolyfill(): typeof promisify; - const implementation: typeof promisify; - function shim(): typeof promisify; + const custom: typeof polyfill.custom; + + /** + * @deprecated + * Not exposed by native `util.promisify` or supported by browserify's `util.promisify`. + * + * Use `util.promisify.custom` instead. + */ + const customPromisifyArgs: typeof polyfill.customPromisifyArgs | undefined; + + function getPolyfill(): ReturnType; + const implementation: typeof polyfill; + function shim(): ReturnType; } export = promisify; diff --git a/types/util.promisify/polyfill.d.ts b/types/util.promisify/polyfill.d.ts index 5056d4e9f4..bef277c3a7 100644 --- a/types/util.promisify/polyfill.d.ts +++ b/types/util.promisify/polyfill.d.ts @@ -1,29 +1,5 @@ -/// - -// 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; +import promisify = require('./implementation'); +import util = require('util'); +declare function getPolyfill(): typeof promisify | typeof util.promisify; export = getPolyfill; diff --git a/types/util.promisify/shim.d.ts b/types/util.promisify/shim.d.ts index 316dc5e996..d478c12bb6 100644 --- a/types/util.promisify/shim.d.ts +++ b/types/util.promisify/shim.d.ts @@ -1,3 +1,4 @@ -import getPolyfill = require("./polyfill"); +import getPolyfill = require('./polyfill'); -export = getPolyfill; +declare function shimUtilPromisify(): ReturnType; +export = shimUtilPromisify; diff --git a/types/util.promisify/tsconfig.json b/types/util.promisify/tsconfig.json index 041c10a94c..bab70d0aab 100644 --- a/types/util.promisify/tsconfig.json +++ b/types/util.promisify/tsconfig.json @@ -1,23 +1,19 @@ { - "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "util.promisify-tests.ts" - ] -} \ No newline at end of file + "compilerOptions": { + "module": "commonjs", + "lib": ["es2015"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "util.promisify-tests.ts", + "index.d.ts" + ] +} diff --git a/types/util.promisify/util.promisify-tests.ts b/types/util.promisify/util.promisify-tests.ts index 50573765bb..93614527a7 100644 --- a/types/util.promisify/util.promisify-tests.ts +++ b/types/util.promisify/util.promisify-tests.ts @@ -4,12 +4,38 @@ import promisify = require('util.promisify'); import 'util.promisify/auto'; -let readFile = promisify(fs.readFile); -readFile = util.promisify(fs.readFile); +/** + * The `expectType` function from https://www.npmjs.com/package/tsd, + * except instead of returning `void`, it returns `T`. + */ +declare function expectType(value: T): T; +expectType(promisify(fs.readFile)); +expectType(util.promisify(fs.readFile)); + +// $ExpectType typeof promisify | typeof promisify let implementation = promisify.getPolyfill(); implementation = promisify.shim(); -implementation = promisify.implementation; -import promisifyShim = require('util.promisify/shim'); -promisifyShim(); +// $ExpectType typeof promisify +const polyfillImpl = promisify.implementation; +implementation = polyfillImpl; + +import getPolyfill = require('util.promisify/polyfill'); +implementation = getPolyfill(); + +import shimUtilPromisify = require('util.promisify/shim'); +implementation = shimUtilPromisify(); + +// The `promisify.custom` symbols are considered equal by the type system: + +expectType(implementation.custom); // $ExpectType typeof custom +expectType(polyfillImpl.custom); // $ExpectType typeof custom +expectType(util.promisify.custom); // $ExpectType typeof custom +expectType(polyfillImpl.custom); // $ExpectType typeof custom +expectType(implementation.custom); // $ExpectType typeof custom +expectType(util.promisify.custom); // $ExpectType typeof custom + +promisify.customPromisifyArgs; // $ExpectType typeof customPromisifyArgs | undefined +polyfillImpl.customPromisifyArgs; // $ExpectType typeof customPromisifyArgs +implementation.customPromisifyArgs; // $ExpectType typeof customPromisifyArgs | undefined