|
| 1 | +import {expectError, expectType, printType} from 'tsd'; |
| 2 | +import pify from '.'; |
| 3 | + |
| 4 | +expectError(pify()); |
| 5 | +expectError(pify(null)); |
| 6 | +expectError(pify(undefined)); |
| 7 | +expectError(pify(123)); |
| 8 | +expectError(pify('abc')); |
| 9 | +expectError(pify(null, {})); |
| 10 | +expectError(pify(undefined, {})); |
| 11 | +expectError(pify(123, {})); |
| 12 | +expectError(pify('abc', {})); |
| 13 | + |
| 14 | +// eslint-disable-next-line @typescript-eslint/no-empty-function |
| 15 | +expectType<Promise<unknown>>(pify((v: number) => {})()); |
| 16 | +expectType<Promise<unknown>>(pify(() => 'hello')()); |
| 17 | + |
| 18 | +// Callback with 1 additional params |
| 19 | +declare function fn1(x: number, fn: (error: Error, value: number) => void): void; |
| 20 | +expectType<Promise<number>>(pify(fn1)(1)); |
| 21 | + |
| 22 | +// Callback with 2 additional params |
| 23 | +declare function fn2(x: number, y: number, fn: (error: Error, value: number) => void): void; |
| 24 | +expectType<Promise<number>>(pify(fn2)(1, 2)); |
| 25 | + |
| 26 | +// Generics |
| 27 | + |
| 28 | +declare function generic<T>(value: T, fn: (error: Error, value: T) => void): void; |
| 29 | +declare const genericValue: 'hello' | 'goodbye'; |
| 30 | +expectType<Promise<typeof genericValue>>(pify(generic)(genericValue)); |
| 31 | + |
| 32 | +declare function generic10<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( |
| 33 | + value1: T1, |
| 34 | + value2: T2, |
| 35 | + value3: T3, |
| 36 | + value4: T4, |
| 37 | + value5: T5, |
| 38 | + value6: T6, |
| 39 | + value7: T7, |
| 40 | + value8: T8, |
| 41 | + value9: T9, |
| 42 | + value10: T10, |
| 43 | + cb: (error: Error, value: { |
| 44 | + val1: T1; |
| 45 | + val2: T2; |
| 46 | + val3: T3; |
| 47 | + val4: T4; |
| 48 | + val5: T5; |
| 49 | + val6: T6; |
| 50 | + val7: T7; |
| 51 | + val8: T8; |
| 52 | + val9: T9; |
| 53 | + val10: T10; |
| 54 | + }) => void |
| 55 | +): void; |
| 56 | +expectType< |
| 57 | +Promise<{ |
| 58 | + val1: 1; |
| 59 | + val2: 2; |
| 60 | + val3: 3; |
| 61 | + val4: 4; |
| 62 | + val5: 5; |
| 63 | + val6: 6; |
| 64 | + val7: 7; |
| 65 | + val8: '8'; |
| 66 | + val9: 9; |
| 67 | + val10: 10; |
| 68 | +}> |
| 69 | +>(pify(generic10)(1, 2, 3, 4, 5, 6, 7, '8', 9, 10)); |
| 70 | + |
| 71 | +// MultiArgs |
| 72 | +declare function callback02(cb: (x: number, y: string) => void): void; |
| 73 | +declare function callback12(value: 'a', cb: (x: number, y: string) => void): void; |
| 74 | +declare function callback22( |
| 75 | + value1: 'a', |
| 76 | + value2: 'b', |
| 77 | + cb: (x: number, y: string) => void |
| 78 | +): void; |
| 79 | + |
| 80 | +expectType<Promise<[number, string]>>(pify(callback02, {multiArgs: true})()); |
| 81 | +expectType<Promise<[number, string]>>( |
| 82 | + pify(callback12, {multiArgs: true})('a'), |
| 83 | +); |
| 84 | +expectType<Promise<[number, string]>>( |
| 85 | + pify(callback22, {multiArgs: true})('a', 'b'), |
| 86 | +); |
| 87 | + |
| 88 | +// Overloads |
| 89 | +declare function overloaded(value: number, cb: (error: Error, value: number) => void): void; |
| 90 | +declare function overloaded(value: string, cb: (error: Error, value: string) => void): void; |
| 91 | + |
| 92 | +// Chooses last overload |
| 93 | +// See https://github.com/microsoft/TypeScript/issues/32164 |
| 94 | +expectType<Promise<string>>(pify(overloaded)('')); |
| 95 | + |
| 96 | +declare const fixtureModule: { |
| 97 | + method1: (arg: string, cb: (error: Error, value: string) => void) => void; |
| 98 | + method2: (arg: number, cb: (error: Error, value: number) => void) => void; |
| 99 | + method3: (arg: string) => string; |
| 100 | + methodSync: (arg: 'sync') => 'sync'; |
| 101 | + methodStream: (arg: 'stream') => 'stream'; |
| 102 | + callbackEndingInSync: (arg: 'sync', cb: (error: Error, value: 'sync') => void) => void; |
| 103 | + prop: number; |
| 104 | +}; |
| 105 | + |
| 106 | +// Module support |
| 107 | +expectType<number>(pify(fixtureModule).prop); |
| 108 | +expectType<Promise<string>>(pify(fixtureModule).method1('')); |
| 109 | +expectType<Promise<number>>(pify(fixtureModule).method2(0)); |
| 110 | +// Same semantics as pify(fn) |
| 111 | +expectType<Promise<unknown>>(pify(fixtureModule).method3()); |
| 112 | + |
| 113 | +// Excludes |
| 114 | +expectType< |
| 115 | +(arg: string, cb: (error: Error, value: string) => void) => void |
| 116 | +>(pify(fixtureModule, {exclude: ['method1']}).method1); |
| 117 | + |
| 118 | +// Includes |
| 119 | +expectType<Promise<string>>(pify(fixtureModule, {include: ['method1']}).method1('')); |
| 120 | +expectType<Promise<number>>(pify(fixtureModule, {include: ['method2']}).method2(0)); |
| 121 | + |
| 122 | +// Excludes sync and stream method by default |
| 123 | +expectType< |
| 124 | +(arg: 'sync') => 'sync' |
| 125 | +>(pify(fixtureModule, {exclude: ['method1']}).methodSync); |
| 126 | +expectType< |
| 127 | +(arg: 'stream') => 'stream' |
| 128 | +>(pify(fixtureModule, {exclude: ['method1']}).methodStream); |
| 129 | + |
| 130 | +// Include sync method |
| 131 | +expectType< |
| 132 | +(arg: 'sync') => Promise<'sync'> |
| 133 | +>(pify(fixtureModule, {include: ['callbackEndingInSync']}).callbackEndingInSync); |
| 134 | + |
| 135 | +// Option errorFirst: |
| 136 | + |
| 137 | +declare function fn0(fn: (value: number) => void): void; |
| 138 | + |
| 139 | +// Unknown as it returns a promise that always rejects because errorFirst = true |
| 140 | +expectType<Promise<unknown>>(pify(fn0)()); |
| 141 | +expectType<Promise<unknown>>(pify(fn0, {errorFirst: true})()); |
| 142 | + |
| 143 | +expectType<Promise<number>>(pify(fn0, {errorFirst: false})()); |
| 144 | +expectType<Promise<[number, string]>>(pify(callback02, {multiArgs: true, errorFirst: true})()); |
| 145 | +expectType<Promise<[number, string]>>( |
| 146 | + pify(callback12, {multiArgs: true, errorFirst: false})('a'), |
| 147 | +); |
| 148 | +expectType<Promise<[number, string]>>( |
| 149 | + pify(callback22, {multiArgs: true, errorFirst: false})('a', 'b'), |
| 150 | +); |
| 151 | + |
| 152 | +// Module function |
| 153 | + |
| 154 | +// eslint-disable-next-line @typescript-eslint/no-empty-function |
| 155 | +function moduleFunction(_cb: (error: Error, value: number) => void): void {} |
| 156 | +// eslint-disable-next-line @typescript-eslint/no-empty-function |
| 157 | +moduleFunction.method = function (_cb: (error: Error, value: string) => void): void {}; |
| 158 | + |
| 159 | +expectType<Promise<number>>(pify(moduleFunction)()); |
| 160 | + |
| 161 | +expectType<Promise<string>>(pify(moduleFunction, {excludeMain: true}).method()); |
| 162 | + |
| 163 | +// Classes |
| 164 | + |
| 165 | +declare class MyClass { |
| 166 | + method1(cb: (error: Error, value: string) => void): void; |
| 167 | + method2(arg: number, cb: (error: Error, value: number) => void): void; |
| 168 | +} |
| 169 | + |
| 170 | +expectType<Promise<string>>(pify(new MyClass()).method1()); |
| 171 | +expectType<Promise<number>>(pify(new MyClass()).method2(4)); |
0 commit comments