From 604b106f0561f3bde0d4afb6843dd00e867c9012 Mon Sep 17 00:00:00 2001 From: Jason Walton Date: Thu, 29 Nov 2018 14:11:39 -0500 Subject: [PATCH] fix: Better typescript definitions. --- index.d.ts | 134 +++++++++++++++++++++++++++++++++++++++++-------- test/tsTest.ts | 28 +++++++++++ 2 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 test/tsTest.ts diff --git a/index.d.ts b/index.d.ts index f1edcf9..77d320b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,32 +1,122 @@ // Global variable exported in browsers. export as namespace promiseBreaker; -interface MakeBreakOptions { - args?: number; -} +declare namespace PromiseBreaker { + interface MakeBreakOptions { + args?: number; + } -interface PromiseBreakerInstance { - make(options: MakeBreakOptions, asyncFn: Function): Function; - make(asyncFn: Function): Function; - break(options: MakeBreakOptions, promiseFn: Function): Function; - break(promiseFn: Function): Function; + type Callback = (err: Error | null | undefined, result?: R) => void; - addPromise(done: Function | undefined | null, asyncFn: Function): Promise | void; + interface BrokenFn0 { + (): Promise; + (cb: Callback): void; + } - addCallback( - done: Function | undefined | null, - promise: (() => Promise) | (() => T) | Promise - ): Promise | void; + interface BrokenFn1 { + (p1: T1): Promise; + (p1: T1, cb: Callback): void; + } - apply(fn: Function, thisArg?: any, args?: any[] | undefined) : Promise; - apply(fn: Function, thisArg: any, args: any[] | undefined, done: Function) : void; - call(fn: Function, thisArg?: any, ...parameters: any[]) : Promise; - callWithCb(fn: Function, thisArg: any, ...parametersAndCallback: any[]): void; -} + interface BrokenFn2 { + (p1: T1, p2: T2): Promise; + (p1: T1, p2: T2, cb: Callback): void; + } + + interface BrokenFn3 { + (p1: T1, p2: T2, p3: T3): Promise; + (p1: T1, p2: T2, p3: T3, cb: Callback): void; + } + + interface PromiseBreakerInstance { + make(options: MakeBreakOptions, asyncFn: (cb: Callback) => void): BrokenFn0; + make( + options: MakeBreakOptions, + asyncFn: (p1: T1, cb: Callback) => void + ): BrokenFn1; + make( + options: MakeBreakOptions, + asyncFn: (p1: T1, p2: T2, cb: Callback) => void + ): BrokenFn2; + make( + options: MakeBreakOptions, + asyncFn: (p1: T1, p2: T2, p3: T3, cb: Callback) => void + ): BrokenFn3; + make( + options: MakeBreakOptions, + asyncFn: (...args: any[]) => any + ): (...args: any[]) => Promise; + + make(asyncFn: (cb: Callback) => void): BrokenFn0; + make(asyncFn: (p1: T1, cb: Callback) => void): BrokenFn1; + make(asyncFn: (p1: T1, p2: T2, cb: Callback) => void): BrokenFn2; + make( + asyncFn: (p1: T1, p2: T2, p3: T3, cb: Callback) => void + ): BrokenFn3; + make(asyncFn: (...args: any[]) => any): (...args: any[]) => Promise; + + break(options: MakeBreakOptions, promiseFn: () => PromiseLike): BrokenFn0; + break( + options: MakeBreakOptions, + promiseFn: (p1: T1) => PromiseLike + ): BrokenFn1; + break( + options: MakeBreakOptions, + promiseFn: (p1: T1, p2: T2) => PromiseLike + ): BrokenFn2; + break( + options: MakeBreakOptions, + promiseFn: (p1: T1, p2: T2, p3: T3) => PromiseLike + ): BrokenFn3; + break( + options: MakeBreakOptions, + promiseFn: (...args: any[]) => any + ): (...args: any[]) => Promise; + + break(promiseFn: () => PromiseLike): BrokenFn0; + break(promiseFn: (p1: T1) => PromiseLike): BrokenFn1; + break(promiseFn: (p1: T1, p2: T2) => PromiseLike): BrokenFn2; + break( + promiseFn: (p1: T1, p2: T2, p3: T3) => PromiseLike + ): BrokenFn3; + break(promiseFn: (...args: any[]) => any): (...args: any[]) => Promise; + + addPromise( + done: Callback | undefined | null, + asyncFn: () => Promise + ): BrokenFn0; + addPromise( + done: Callback | undefined | null, + asyncFn: (p1: T1) => Promise + ): BrokenFn1; + addPromise( + done: Callback | undefined | null, + asyncFn: (p1: T1, p2: T2) => Promise + ): BrokenFn2; + addPromise( + done: Callback | undefined | null, + asyncFn: (p1: T1, p2: T2, p3: T3) => Promise + ): BrokenFn3; + addPromise( + done: Callback | undefined | null, + asyncFn: (...args: any[]) => Promise + ): Promise; + + addCallback( + done: Callback | undefined | null, + promise: (() => Promise) | (() => R) | Promise + ): Promise; + + apply(fn: Function, thisArg?: any, args?: any[] | undefined): Promise; + apply(fn: Function, thisArg: any, args: any[] | undefined, done: Function): void; + call(fn: Function, thisArg?: any, ...parameters: any[]): Promise; + callWithCb(fn: Function, thisArg: any, ...parametersAndCallback: any[]): void; + } -interface PromiseBreaker extends PromiseBreakerInstance { - withPromise(promiseImpl: PromiseConstructor) : PromiseBreaker; + interface PromiseBreaker extends PromiseBreakerInstance { + withPromise(promiseImpl: PromiseConstructor): PromiseBreaker; + } } -declare const usingDefaultPromise: PromiseBreaker; -export = usingDefaultPromise; +declare const PromiseBreaker: PromiseBreaker.PromiseBreaker; +export = PromiseBreaker; diff --git a/test/tsTest.ts b/test/tsTest.ts new file mode 100644 index 0000000..14dcbb2 --- /dev/null +++ b/test/tsTest.ts @@ -0,0 +1,28 @@ + +import pb from '..'; + +const make0 = pb.make((cb: (err?: Error, result?: string) => void) => + cb(undefined, 'hello') +); +make0().then(result => console.log(result)); + +const make1 = pb.make((a: string, cb: (err?: Error, result?: string) => void) => + cb(undefined, 'hello') +); +make1('a').then(result => console.log(result)); + + +const make2 = pb.make((a: string, b: number, cb: (err?: Error, result?: string) => void) => + cb(undefined, 'hello' + a) +); +make2('jason', 2).then(result => console.log(result)); + +const make3 = pb.make((a: string, b: number, c: string, cb: (err?: Error, result?: string) => void) => + cb(undefined, 'hello' + a) +); +make3('jason', 2, 'foo').then(result => console.log(result)); + +const make4 = pb.make((a: string, b: number, c: string, d: number, cb: (err?: Error, result?: string) => void) => + cb(undefined, 'hello' + a) +); +make4('jason', 2, 'foo', 10).then(result => console.log(result));