From 70956465acf34470fa1c362d6961bbf03bb4b5bb Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Wed, 19 Feb 2020 20:13:44 +0100 Subject: [PATCH] =?UTF-8?q?feat(node):=20Add=C2=A0support=20for=C2=A0symbo?= =?UTF-8?q?l-based=20`CustomPromisify`=20(#42154)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(node): Add support for symbol-based `CustomPromisify` * test(node): Add tests for symbol‑based `CustomPromisify` * test(node): Fix `doSomething`’s `onSuccessCallback` defintion --- types/node/node-tests.ts | 11 ++++++++++- types/node/util.d.ts | 21 +++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index f9dcb5e19d..12c2bad9a5 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -130,7 +130,10 @@ import * as trace_events from "trace_events"; const b: boolean = timeout.hasRef(); timers.clearTimeout(timeout); } - async function testPromisify() { + async function testPromisify(doSomething: { + (foo: any, onSuccessCallback: (result: string) => void, onErrorCallback: (reason: any) => void): void; + [util.promisify.custom](foo: any): Promise; + }) { const setTimeout = util.promisify(timers.setTimeout); let v: void = await setTimeout(100); // tslint:disable-line no-void-expression void-return let s: string = await setTimeout(100, ""); @@ -138,6 +141,12 @@ import * as trace_events from "trace_events"; const setImmediate = util.promisify(timers.setImmediate); v = await setImmediate(); // tslint:disable-line no-void-expression s = await setImmediate(""); + + // $ExpectType (foo: any) => Promise + const doSomethingPromise = util.promisify(doSomething); + + // $ExpectType string + s = await doSomethingPromise('foo'); } } diff --git a/types/node/util.d.ts b/types/node/util.d.ts index 85a5ad8138..e1507b9c81 100644 --- a/types/node/util.d.ts +++ b/types/node/util.d.ts @@ -60,10 +60,6 @@ declare module "util" { function deprecate(fn: T, message: string, code?: string): T; function isDeepStrictEqual(val1: any, val2: any): boolean; - interface CustomPromisify extends Function { - __promisify__: TCustom; - } - function callbackify(fn: () => Promise): (callback: (err: NodeJS.ErrnoException) => void) => void; function callbackify(fn: () => Promise): (callback: (err: NodeJS.ErrnoException, result: TResult) => void) => void; function callbackify(fn: (arg1: T1) => Promise): (arg1: T1, callback: (err: NodeJS.ErrnoException) => void) => void; @@ -89,6 +85,16 @@ declare module "util" { fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6) => Promise ): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, callback: (err: NodeJS.ErrnoException | null, result: TResult) => void) => void; + interface CustomPromisifyLegacy extends Function { + __promisify__: TCustom; + } + + interface CustomPromisifySymbol extends Function { + [promisify.custom]: TCustom; + } + + type CustomPromisify = CustomPromisifySymbol | CustomPromisifyLegacy; + function promisify(fn: CustomPromisify): TCustom; function promisify(fn: (callback: (err: any, result: TResult) => void) => void): () => Promise; function promisify(fn: (callback: (err?: any) => void) => void): () => Promise; @@ -111,6 +117,9 @@ declare module "util" { 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; function promisify(fn: Function): Function; + namespace promisify { + const custom: unique symbol; + } namespace types { function isAnyArrayBuffer(object: any): boolean; @@ -183,8 +192,4 @@ declare module "util" { encode(input?: string): Uint8Array; encodeInto(input: string, output: Uint8Array): EncodeIntoResult; } - - namespace promisify { - const custom: unique symbol; - } }