Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
More obvious tests

Generic af

Saner tests

WIP

WIP
  • Loading branch information
tom-sherman committed Aug 31, 2022
1 parent 513b0d5 commit 7a3a559
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
30 changes: 30 additions & 0 deletions index.d.ts
@@ -0,0 +1,30 @@
type Last<T extends readonly unknown[]> = T extends [...any, infer L]
? L
: never;
type DropLast<T extends readonly unknown[]> = T extends [...(infer U), any]
? U
: [];

interface Options {
multiArgs: boolean;
}

type Promisify<TArgs extends readonly unknown[], TOptions extends Options> = (
...args: DropLast<TArgs>
) => Last<TArgs> extends (...args: any) => any
? Promise<
TOptions extends { multiArgs: false }
? Last<Parameters<Last<TArgs>>>
: Parameters<Last<TArgs>>
>
: never;

declare function pify<
TArgs extends readonly unknown[],
TOptions extends Options = { multiArgs: false }
>(
input: (...args: TArgs) => any,
options?: TOptions
): Promisify<TArgs, TOptions>;

export = pify;
96 changes: 96 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,96 @@
import { expectError, expectType } from "tsd";
import pify = require(".");

expectError(pify());
expectError(pify(null));
expectError(pify(undefined));
expectError(pify(123));
expectError(pify("abc"));
expectError(pify(null, {}));
expectError(pify(undefined, {}));
expectError(pify(123, {}));
expectError(pify("abc", {}));

expectType<never>(pify((v: number) => {})());

// callback with 0 additional params
declare function fn0(fn: (val: number) => void): void;
expectType<Promise<number>>(pify(fn0)());

// callback with 1 additional params
declare function fn1(x: number, fn: (val: number) => void): void;
expectType<Promise<number>>(pify(fn1)(1));

// callback with 2 additional params
declare function fn2(x: number, y: number, fn: (val: number) => void): void;
expectType<Promise<number>>(pify(fn2)(1, 2));

// generics

declare function generic<T>(val: T, fn: (val: T) => void): void;
declare const genericVal: "hello" | "goodbye";
expectType<Promise<typeof genericVal>>(pify(generic)(genericVal));

declare function generic10<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
val1: T1,
val2: T2,
val3: T3,
val4: T4,
val5: T5,
val6: T6,
val7: T7,
val8: T8,
val9: T9,
val10: T10,
cb: (value: {
val1: T1;
val2: T2;
val3: T3;
val4: T4;
val5: T5;
val6: T6;
val7: T7;
val8: T8;
val9: T9;
val10: T10;
}) => void
): void;
expectType<
Promise<{
val1: 1;
val2: 2;
val3: 3;
val4: 4;
val5: 5;
val6: 6;
val7: 7;
val8: "8";
val9: 9;
val10: 10;
}>
>(pify(generic10)(1, 2, 3, 4, 5, 6, 7, "8", 9, 10));

// multiArgs
declare function callback02(cb: (x: number, y: string) => void): void;
declare function callback12(val: "a", cb: (x: number, y: string) => void): void;
declare function callback22(
val1: "a",
val2: "b",
cb: (x: number, y: string) => void
): void;

expectType<Promise<[number, string]>>(pify(callback02, { multiArgs: true })());
expectType<Promise<[number, string]>>(
pify(callback12, { multiArgs: true })("a")
);
expectType<Promise<[number, string]>>(
pify(callback22, { multiArgs: true })("a", "b")
);

// overloads
declare function overloaded(value: number, cb: (value: number) => void): void;
declare function overloaded(value: string, cb: (value: string) => void): void;

// Chooses last overload
// See https://github.com/microsoft/TypeScript/issues/32164
expectType<Promise<string>>(pify(overloaded)(""));
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -16,11 +16,12 @@
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava",
"test": "xo && ava && tsd",
"optimization-test": "node --allow-natives-syntax optimization-test.js"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"promisify",
Expand All @@ -45,6 +46,8 @@
"devDependencies": {
"ava": "^4.3.0",
"pinkie-promise": "^2.0.1",
"tsd": "^0.19.0",
"typescript": "^4.5.4",
"v8-natives": "^1.2.5",
"xo": "^0.49.0"
}
Expand Down
5 changes: 5 additions & 0 deletions tsconfig.json
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strict": true
}
}

0 comments on commit 7a3a559

Please sign in to comment.