Refactor TypeScript definition to CommonJS compatible export (#4)

This commit is contained in:
Dimitri Benin 2019-04-03 05:19:13 +00:00 committed by Sindre Sorhus
parent d43a753a44
commit 23a0139209
4 changed files with 55 additions and 24 deletions

67
index.d.ts vendored
View File

@ -1,25 +1,54 @@
export interface DeferredPromise<ValueType> {
/**
Resolves the promise with a value or the result of another promise.
declare namespace pDefer {
interface DeferredPromise<ValueType> {
/**
Resolves the promise with a value or the result of another promise.
@param value - The value to resolve the promise with.
*/
resolve(value?: ValueType | PromiseLike<ValueType>): void;
@param value - The value to resolve the promise with.
*/
resolve(value?: ValueType | PromiseLike<ValueType>): void;
/**
Reject the promise with a provided reason or error.
/**
Reject the promise with a provided reason or error.
@param reason - The reason or error to reject the promise with.
*/
reject(reason?: unknown): void;
@param reason - The reason or error to reject the promise with.
*/
reject(reason?: unknown): void;
/**
The deferred promise.
*/
promise: Promise<ValueType>;
/**
The deferred promise.
*/
promise: Promise<ValueType>;
}
}
/**
Create a deferred promise.
*/
export default function pDefer<ValueType = unknown>(): DeferredPromise<ValueType>;
declare const pDefer: {
/**
Create a deferred promise.
@example
```
import pDefer = require('p-defer');
function delay(ms) {
const deferred = pDefer();
setTimeout(deferred.resolve, ms, '🦄');
return deferred.promise;
}
(async () => {
console.log(await delay(100));
//=> '🦄'
})();
```
*/
<ValueType = unknown>(): pDefer.DeferredPromise<ValueType>;
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pDefer<ValueType = unknown>(): pDefer.DeferredPromise<
// ValueType
// >;
// export = pDefer;
default: typeof pDefer;
};
export = pDefer;

View File

@ -12,4 +12,5 @@ const pDefer = () => {
};
module.exports = pDefer;
// TODO: Remove this for the next major release
module.exports.default = pDefer;

View File

@ -1,5 +1,6 @@
import {expectType} from 'tsd-check';
import pDefer, {DeferredPromise} from '.';
import {expectType} from 'tsd';
import pDefer = require('.');
import {DeferredPromise} from '.';
expectType<DeferredPromise<unknown>>(pDefer());
expectType<DeferredPromise<string>>(pDefer<string>());

View File

@ -13,7 +13,7 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
@ -33,8 +33,8 @@
"bluebird"
],
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.5.0",
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}