fix(util.promisify): Widen err param and add implementation.d.ts (#42130)

This commit is contained in:
ExE Boss 2020-02-06 18:30:50 +01:00 committed by GitHub
parent e3b0532870
commit 6531f81022
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 181 additions and 78 deletions

View File

@ -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;
}
}

View File

@ -0,0 +1,59 @@
import util = require('util');
// tslint:disable-next-line: ban-types
declare function promisify<TCustom extends Function>(fn: util.CustomPromisify<TCustom>): TCustom;
declare function promisify<TResult>(
fn: (callback: (err: any, result: TResult) => void) => void,
): () => Promise<TResult>;
declare function promisify(fn: (callback: (err?: any) => void) => void): () => Promise<void>;
declare function promisify<T1, TResult>(
fn: (arg1: T1, callback: (err: any, result: TResult) => void) => void,
): (arg1: T1) => Promise<TResult>;
declare function promisify<T1>(fn: (arg1: T1, callback: (err?: any) => void) => void): (arg1: T1) => Promise<void>;
declare function promisify<T1, T2, TResult>(
fn: (arg1: T1, arg2: T2, callback: (err: any, result: TResult) => void) => void,
): (arg1: T1, arg2: T2) => Promise<TResult>;
declare function promisify<T1, T2>(
fn: (arg1: T1, arg2: T2, callback: (err?: any) => void) => void,
): (arg1: T1, arg2: T2) => Promise<void>;
declare function promisify<T1, T2, T3, TResult>(
fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: any, result: TResult) => void) => void,
): (arg1: T1, arg2: T2, arg3: T3) => Promise<TResult>;
declare function promisify<T1, T2, T3>(
fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: any) => void) => void,
): (arg1: T1, arg2: T2, arg3: T3) => Promise<void>;
declare function promisify<T1, T2, T3, T4, TResult>(
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<TResult>;
declare function promisify<T1, T2, T3, T4>(
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err?: any) => void) => void,
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>;
declare function promisify<T1, T2, T3, T4, T5, TResult>(
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<TResult>;
declare function promisify<T1, T2, T3, T4, T5>(
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<void>;
// 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;

View File

@ -2,40 +2,75 @@
// Project: https://github.com/ljharb/util.promisify#readme
// Definitions by: Adam Voss <https://github.com/adamvoss>
// Piotr Roszatycki <https://github.com/dex4er>
// ExE Boss <https://github.com/ExE-Boss>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
// tslint:disable:ban-types
interface CustomPromisify<TCustom extends Function> 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<TCustom extends Function>(fn: util.CustomPromisify<TCustom>): TCustom;
declare function promisify<TResult>(
fn: (callback: (err: any, result: TResult) => void) => void,
): () => Promise<TResult>;
declare function promisify(fn: (callback: (err?: any) => void) => void): () => Promise<void>;
declare function promisify<T1, TResult>(
fn: (arg1: T1, callback: (err: any, result: TResult) => void) => void,
): (arg1: T1) => Promise<TResult>;
declare function promisify<T1>(fn: (arg1: T1, callback: (err?: any) => void) => void): (arg1: T1) => Promise<void>;
declare function promisify<T1, T2, TResult>(
fn: (arg1: T1, arg2: T2, callback: (err: any, result: TResult) => void) => void,
): (arg1: T1, arg2: T2) => Promise<TResult>;
declare function promisify<T1, T2>(
fn: (arg1: T1, arg2: T2, callback: (err?: any) => void) => void,
): (arg1: T1, arg2: T2) => Promise<void>;
declare function promisify<T1, T2, T3, TResult>(
fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: any, result: TResult) => void) => void,
): (arg1: T1, arg2: T2, arg3: T3) => Promise<TResult>;
declare function promisify<T1, T2, T3>(
fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: any) => void) => void,
): (arg1: T1, arg2: T2, arg3: T3) => Promise<void>;
declare function promisify<TCustom extends Function>(fn: CustomPromisify<TCustom>): TCustom;
declare function promisify<TResult>(fn: (callback: (err: Error | null, result: TResult) => void) => void): () => Promise<TResult>;
declare function promisify(fn: (callback: (err?: Error | null) => void) => void): () => Promise<void>;
declare function promisify<T1, TResult>(fn: (arg1: T1, callback: (err: Error | null, result: TResult) => void) => void): (arg1: T1) => Promise<TResult>;
declare function promisify<T1>(fn: (arg1: T1, callback: (err?: Error | null) => void) => void): (arg1: T1) => Promise<void>;
declare function promisify<T1, T2, TResult>(fn: (arg1: T1, arg2: T2, callback: (err: Error | null, result: TResult) => void) => void): (arg1: T1, arg2: T2) => Promise<TResult>;
declare function promisify<T1, T2>(fn: (arg1: T1, arg2: T2, callback: (err?: Error | null) => void) => void): (arg1: T1, arg2: T2) => Promise<void>;
declare function promisify<T1, T2, T3, TResult>(fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: Error | null, result: TResult) => void) => void): (arg1: T1, arg2: T2, arg3: T3) => Promise<TResult>;
declare function promisify<T1, T2, T3>(fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: Error | null) => void) => void): (arg1: T1, arg2: T2, arg3: T3) => Promise<void>;
declare function promisify<T1, T2, T3, T4, TResult>(
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<TResult>;
declare function promisify<T1, T2, T3, T4>(fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err?: Error | null) => void) => void): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>;
declare function promisify<T1, T2, T3, T4>(
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err?: any) => void) => void,
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>;
declare function promisify<T1, T2, T3, T4, T5, TResult>(
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<TResult>;
declare function promisify<T1, T2, T3, T4, T5>(
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<void>;
// 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<typeof getUtilPromisify>;
const implementation: typeof polyfill;
function shim(): ReturnType<typeof shimUtilPromisify>;
}
export = promisify;

View File

@ -1,29 +1,5 @@
/// <reference types="node" />
// tslint:disable:ban-types max-line-length
interface CustomPromisify<TCustom extends Function> extends Function {
__promisify__: TCustom;
}
declare function getPolyfill<TCustom extends Function>(): (fn: CustomPromisify<TCustom>) => TCustom;
declare function getPolyfill<TResult>(): (fn: (callback: (err: Error | null, result: TResult) => void) => void) => () => Promise<TResult>;
declare function getPolyfill(): (fn: (callback: (err?: Error | null) => void) => void) => () => Promise<void>;
declare function getPolyfill<T1, TResult>(): (fn: (arg1: T1, callback: (err: Error | null, result: TResult) => void) => void) => (arg1: T1) => Promise<TResult>;
declare function getPolyfill<T1>(): (fn: (arg1: T1, callback: (err?: Error | null) => void) => void) => (arg1: T1) => Promise<void>;
declare function getPolyfill<T1, T2, TResult>(): (fn: (arg1: T1, arg2: T2, callback: (err: Error | null, result: TResult) => void) => void) => (arg1: T1, arg2: T2) => Promise<TResult>;
declare function getPolyfill<T1, T2>(): (fn: (arg1: T1, arg2: T2, callback: (err?: Error | null) => void) => void) => (arg1: T1, arg2: T2) => Promise<void>;
declare function getPolyfill<T1, T2, T3, TResult>(): (fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err: Error | null, result: TResult) => void) => void) => (arg1: T1, arg2: T2, arg3: T3) => Promise<TResult>;
declare function getPolyfill<T1, T2, T3>(): (fn: (arg1: T1, arg2: T2, arg3: T3, callback: (err?: Error | null) => void) => void) => (arg1: T1, arg2: T2, arg3: T3) => Promise<void>;
declare function getPolyfill<T1, T2, T3, T4, TResult>(): (
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<TResult>;
declare function getPolyfill<T1, T2, T3, T4>(): (fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err?: Error | null) => void) => void) => (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => Promise<void>;
declare function getPolyfill<T1, T2, T3, T4, T5, TResult>(): (
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<TResult>;
declare function getPolyfill<T1, T2, T3, T4, T5>(): (
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<void>;
declare function getPolyfill(): (fn: Function) => Function;
import promisify = require('./implementation');
import util = require('util');
declare function getPolyfill(): typeof promisify | typeof util.promisify;
export = getPolyfill;

View File

@ -1,3 +1,4 @@
import getPolyfill = require("./polyfill");
import getPolyfill = require('./polyfill');
export = getPolyfill;
declare function shimUtilPromisify(): ReturnType<typeof getPolyfill>;
export = shimUtilPromisify;

View File

@ -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"
]
}
"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"
]
}

View File

@ -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<T>(value: T): T;
expectType<typeof fs.readFile.__promisify__>(promisify(fs.readFile));
expectType<typeof fs.readFile.__promisify__>(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<typeof util.promisify.custom>(implementation.custom); // $ExpectType typeof custom
expectType<typeof util.promisify.custom>(polyfillImpl.custom); // $ExpectType typeof custom
expectType<typeof implementation.custom>(util.promisify.custom); // $ExpectType typeof custom
expectType<typeof implementation.custom>(polyfillImpl.custom); // $ExpectType typeof custom
expectType<typeof polyfillImpl.custom>(implementation.custom); // $ExpectType typeof custom
expectType<typeof polyfillImpl.custom>(util.promisify.custom); // $ExpectType typeof custom
promisify.customPromisifyArgs; // $ExpectType typeof customPromisifyArgs | undefined
polyfillImpl.customPromisifyArgs; // $ExpectType typeof customPromisifyArgs
implementation.customPromisifyArgs; // $ExpectType typeof customPromisifyArgs | undefined