Skip to content

Commit

Permalink
Refactor TypeScript definition to CommonJS compatible export (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 8, 2019
1 parent 3217a33 commit fa2d83c
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 68 deletions.
120 changes: 71 additions & 49 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,86 @@
export interface ClearablePromise<T> extends Promise<T> {
/**
* Clears the delay and settles the promise.
*/
clear(): void;
}

/**
* Minimal subset of `AbortSignal` that delay will use if passed.
* This avoids a dependency on dom.d.ts.
* The dom.d.ts `AbortSignal` is compatible with this one.
*/
interface AbortSignal {
readonly aborted: boolean;
addEventListener(type: 'abort', listener: () => void, options?: { once?: boolean }): void;
removeEventListener(type: 'abort', listener: () => void): void;
}
declare namespace delay {
interface ClearablePromise<T> extends Promise<T> {
/**
Clears the delay and settles the promise.
*/
clear(): void;
}

export interface Options {
/**
* An optional AbortSignal to abort the delay.
* If aborted, the Promise will be rejected with an AbortError.
*/
signal?: AbortSignal
Minimal subset of `AbortSignal` that delay will use if passed.
This avoids a dependency on dom.d.ts.
The dom.d.ts `AbortSignal` is compatible with this one.
*/
interface AbortSignal {
readonly aborted: boolean;
addEventListener(
type: 'abort',
listener: () => void,
options?: {once?: boolean}
): void;
removeEventListener(type: 'abort', listener: () => void): void;
}

interface Options {
/**
An optional AbortSignal to abort the delay.
If aborted, the Promise will be rejected with an AbortError.
*/
signal?: AbortSignal;
}
}

type Delay = {
/**
* Create a promise which resolves after the specified `milliseconds`.
*
* @param milliseconds - Milliseconds to delay the promise.
* @returns A promise which resolves after the specified `milliseconds`.
*/
(milliseconds: number, options?: Options): ClearablePromise<void>;
Create a promise which resolves after the specified `milliseconds`.
@param milliseconds - Milliseconds to delay the promise.
@returns A promise which resolves after the specified `milliseconds`.
*/
(milliseconds: number, options?: delay.Options): delay.ClearablePromise<void>;

/**
* Create a promise which resolves after the specified `milliseconds`.
*
* @param milliseconds - Milliseconds to delay the promise.
* @returns A promise which resolves after the specified `milliseconds`.
*/
<T>(milliseconds: number, options?: Options & {
/** Value to resolve in the returned promise. */
value: T
}): ClearablePromise<T>;
Create a promise which resolves after the specified `milliseconds`.
@param milliseconds - Milliseconds to delay the promise.
@returns A promise which resolves after the specified `milliseconds`.
*/
<T>(
milliseconds: number,
options?: delay.Options & {
/**
Value to resolve in the returned promise.
*/
value: T;
}
): delay.ClearablePromise<T>;

/**
* Create a promise which rejects after the specified `milliseconds`.
*
* @param milliseconds - Milliseconds to delay the promise.
* @returns A promise which rejects after the specified `milliseconds`.
*/
Create a promise which rejects after the specified `milliseconds`.
@param milliseconds - Milliseconds to delay the promise.
@returns A promise which rejects after the specified `milliseconds`.
*/
// TODO: Allow providing value type after https://github.com/Microsoft/TypeScript/issues/5413 will be resolved.
reject(milliseconds: number, options?: Options & {
/** Value to reject in the returned promise. */
value?: any
}): ClearablePromise<never>;
}
reject(
milliseconds: number,
options?: delay.Options & {
/**
Value to reject in the returned promise.
*/
value?: unknown;
}
): delay.ClearablePromise<never>;
};

declare const delay: Delay & {
createWithTimers(timers: {clearTimeout: typeof clearTimeout, setTimeout: typeof setTimeout}): Delay
createWithTimers(timers: {
clearTimeout: typeof clearTimeout;
setTimeout: typeof setTimeout;
}): Delay;

// TODO: Remove this for the next major release
default: typeof delay;
};

export default delay;
export = delay;
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const createDelay = ({clearTimeout: clear = clearTimeout, setTimeout: set = setT
reject(value);
}
};

rejectFn = reject;
timeoutId = set(settle, ms);
});
Expand All @@ -60,5 +61,7 @@ delay.createWithTimers = ({clearTimeout, setTimeout}) => {
delay.reject = createDelay({clearTimeout, setTimeout, willResolve: false});
return delay;
};

module.exports = delay;
// TODO: Remove this for the next major release
module.exports.default = delay;
31 changes: 18 additions & 13 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import {expectType} from 'tsd-check';
import delay from '.';
/// <reference lib="dom"/>
import {expectType} from 'tsd';
import delay = require('.');
import {ClearablePromise} from '.';

expectType<void>(await delay(200));
expectType<ClearablePromise<void>>(delay(200));

expectType<string>(await delay(200, {value: '🦄'}));
expectType<number>(await delay(200, {value: 0}));
expectType<ClearablePromise<string>>(delay(200, {value: '🦄'}));
expectType<ClearablePromise<number>>(delay(200, {value: 0}));
expectType<ClearablePromise<void>>(
delay(200, {signal: new AbortController().signal})
);

expectType<never>(await delay.reject(200, {value: '🦄'}));
expectType<never>(await delay.reject(200, {value: 0}));
expectType<ClearablePromise<never>>(delay.reject(200, {value: '🦄'}));
expectType<ClearablePromise<never>>(delay.reject(200, {value: 0}));

const customDelay = delay.createWithTimers({clearTimeout, setTimeout})
expectType<void>(await customDelay(200));
const customDelay = delay.createWithTimers({clearTimeout, setTimeout});
expectType<ClearablePromise<void>>(customDelay(200));

expectType<string>(await customDelay(200, {value: '🦄'}));
expectType<number>(await customDelay(200, {value: 0}));
expectType<ClearablePromise<string>>(customDelay(200, {value: '🦄'}));
expectType<ClearablePromise<number>>(customDelay(200, {value: 0}));

expectType<never>(await customDelay.reject(200, {value: '🦄'}));
expectType<never>(await customDelay.reject(200, {value: 0}));
expectType<ClearablePromise<never>>(customDelay.reject(200, {value: '🦄'}));
expectType<ClearablePromise<never>>(customDelay.reject(200, {value: 0}));
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -39,12 +39,12 @@
"bluebird"
],
"devDependencies": {
"abort-controller": "^1.0.2",
"ava": "1.0.0-beta.8",
"abort-controller": "^3.0.0",
"ava": "1.4.1",
"currently-unhandled": "^0.4.1",
"in-range": "^1.0.0",
"time-span": "^2.0.0",
"tsd-check": "^0.2.1",
"xo": "*"
"time-span": "^3.0.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}

0 comments on commit fa2d83c

Please sign in to comment.