Skip to content

Commit

Permalink
fix: Better typescript definitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Walton committed Nov 29, 2018
1 parent 79e707e commit 604b106
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 22 deletions.
134 changes: 112 additions & 22 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<R = any> = (err: Error | null | undefined, result?: R) => void;

addPromise<T>(done: Function | undefined | null, asyncFn: Function): Promise<T> | void;
interface BrokenFn0<R> {
(): Promise<R>;
(cb: Callback<R>): void;
}

addCallback<T>(
done: Function | undefined | null,
promise: (() => Promise<T>) | (() => T) | Promise<T>
): Promise<T> | void;
interface BrokenFn1<T1, R> {
(p1: T1): Promise<R>;
(p1: T1, cb: Callback<R>): void;
}

apply(fn: Function, thisArg?: any, args?: any[] | undefined) : Promise<any>;
apply(fn: Function, thisArg: any, args: any[] | undefined, done: Function) : void;
call(fn: Function, thisArg?: any, ...parameters: any[]) : Promise<any>;
callWithCb(fn: Function, thisArg: any, ...parametersAndCallback: any[]): void;
}
interface BrokenFn2<T1, T2, R> {
(p1: T1, p2: T2): Promise<R>;
(p1: T1, p2: T2, cb: Callback<R>): void;
}

interface BrokenFn3<T1, T2, T3, R> {
(p1: T1, p2: T2, p3: T3): Promise<R>;
(p1: T1, p2: T2, p3: T3, cb: Callback<R>): void;
}

interface PromiseBreakerInstance {
make<R>(options: MakeBreakOptions, asyncFn: (cb: Callback<R>) => void): BrokenFn0<R>;
make<T1, R>(
options: MakeBreakOptions,
asyncFn: (p1: T1, cb: Callback<R>) => void
): BrokenFn1<T1, R>;
make<T1, T2, R>(
options: MakeBreakOptions,
asyncFn: (p1: T1, p2: T2, cb: Callback<R>) => void
): BrokenFn2<T1, T2, R>;
make<T1, T2, T3, R>(
options: MakeBreakOptions,
asyncFn: (p1: T1, p2: T2, p3: T3, cb: Callback<R>) => void
): BrokenFn3<T1, T2, T3, R>;
make<R = any>(
options: MakeBreakOptions,
asyncFn: (...args: any[]) => any
): (...args: any[]) => Promise<R>;

make<R>(asyncFn: (cb: Callback<R>) => void): BrokenFn0<R>;
make<T1, R>(asyncFn: (p1: T1, cb: Callback<R>) => void): BrokenFn1<T1, R>;
make<T1, T2, R>(asyncFn: (p1: T1, p2: T2, cb: Callback<R>) => void): BrokenFn2<T1, T2, R>;
make<T1, T2, T3, R>(
asyncFn: (p1: T1, p2: T2, p3: T3, cb: Callback<R>) => void
): BrokenFn3<T1, T2, T3, R>;
make<R = any>(asyncFn: (...args: any[]) => any): (...args: any[]) => Promise<R>;

break<R>(options: MakeBreakOptions, promiseFn: () => PromiseLike<R>): BrokenFn0<R>;
break<T1, R>(
options: MakeBreakOptions,
promiseFn: (p1: T1) => PromiseLike<R>
): BrokenFn1<T1, R>;
break<T1, T2, R>(
options: MakeBreakOptions,
promiseFn: (p1: T1, p2: T2) => PromiseLike<R>
): BrokenFn2<T1, T2, R>;
break<T1, T2, T3, R>(
options: MakeBreakOptions,
promiseFn: (p1: T1, p2: T2, p3: T3) => PromiseLike<R>
): BrokenFn3<T1, T2, T3, R>;
break<R = any>(
options: MakeBreakOptions,
promiseFn: (...args: any[]) => any
): (...args: any[]) => Promise<R>;

break<R>(promiseFn: () => PromiseLike<R>): BrokenFn0<R>;
break<T1, R>(promiseFn: (p1: T1) => PromiseLike<R>): BrokenFn1<T1, R>;
break<T1, T2, R>(promiseFn: (p1: T1, p2: T2) => PromiseLike<R>): BrokenFn2<T1, T2, R>;
break<T1, T2, T3, R>(
promiseFn: (p1: T1, p2: T2, p3: T3) => PromiseLike<R>
): BrokenFn3<T1, T2, T3, R>;
break<R = any>(promiseFn: (...args: any[]) => any): (...args: any[]) => Promise<R>;

addPromise<R>(
done: Callback<R> | undefined | null,
asyncFn: () => Promise<R>
): BrokenFn0<R>;
addPromise<T1, R>(
done: Callback<R> | undefined | null,
asyncFn: (p1: T1) => Promise<R>
): BrokenFn1<T1, R>;
addPromise<T1, T2, R>(
done: Callback<R> | undefined | null,
asyncFn: (p1: T1, p2: T2) => Promise<R>
): BrokenFn2<T1, T2, R>;
addPromise<T1, T2, T3, R>(
done: Callback<R> | undefined | null,
asyncFn: (p1: T1, p2: T2, p3: T3) => Promise<R>
): BrokenFn3<T1, T2, T3, R>;
addPromise<R>(
done: Callback<R> | undefined | null,
asyncFn: (...args: any[]) => Promise<R>
): Promise<R>;

addCallback<R>(
done: Callback<R> | undefined | null,
promise: (() => Promise<R>) | (() => R) | Promise<R>
): Promise<R>;

apply(fn: Function, thisArg?: any, args?: any[] | undefined): Promise<any>;
apply(fn: Function, thisArg: any, args: any[] | undefined, done: Function): void;
call(fn: Function, thisArg?: any, ...parameters: any[]): Promise<any>;
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;
28 changes: 28 additions & 0 deletions test/tsTest.ts
Original file line number Diff line number Diff line change
@@ -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));

0 comments on commit 604b106

Please sign in to comment.