Refactor TypeScript definition to CommonJS compatible export (#4)
This commit is contained in:
parent
d43a753a44
commit
23a0139209
|
@ -1,25 +1,54 @@
|
||||||
export interface DeferredPromise<ValueType> {
|
declare namespace pDefer {
|
||||||
/**
|
interface DeferredPromise<ValueType> {
|
||||||
Resolves the promise with a value or the result of another promise.
|
/**
|
||||||
|
Resolves the promise with a value or the result of another promise.
|
||||||
|
|
||||||
@param value - The value to resolve the promise with.
|
@param value - The value to resolve the promise with.
|
||||||
*/
|
*/
|
||||||
resolve(value?: ValueType | PromiseLike<ValueType>): void;
|
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.
|
@param reason - The reason or error to reject the promise with.
|
||||||
*/
|
*/
|
||||||
reject(reason?: unknown): void;
|
reject(reason?: unknown): void;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
The deferred promise.
|
The deferred promise.
|
||||||
*/
|
*/
|
||||||
promise: Promise<ValueType>;
|
promise: Promise<ValueType>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
declare const pDefer: {
|
||||||
Create a deferred promise.
|
/**
|
||||||
*/
|
Create a deferred promise.
|
||||||
export default function pDefer<ValueType = unknown>(): DeferredPromise<ValueType>;
|
|
||||||
|
@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;
|
||||||
|
|
1
index.js
1
index.js
|
@ -12,4 +12,5 @@ const pDefer = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = pDefer;
|
module.exports = pDefer;
|
||||||
|
// TODO: Remove this for the next major release
|
||||||
module.exports.default = pDefer;
|
module.exports.default = pDefer;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import {expectType} from 'tsd-check';
|
import {expectType} from 'tsd';
|
||||||
import pDefer, {DeferredPromise} from '.';
|
import pDefer = require('.');
|
||||||
|
import {DeferredPromise} from '.';
|
||||||
|
|
||||||
expectType<DeferredPromise<unknown>>(pDefer());
|
expectType<DeferredPromise<unknown>>(pDefer());
|
||||||
expectType<DeferredPromise<string>>(pDefer<string>());
|
expectType<DeferredPromise<string>>(pDefer<string>());
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
"node": ">=6"
|
"node": ">=6"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "xo && ava && tsd-check"
|
"test": "xo && ava && tsd"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"index.js",
|
"index.js",
|
||||||
|
@ -33,8 +33,8 @@
|
||||||
"bluebird"
|
"bluebird"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"ava": "^1.3.1",
|
"ava": "^1.4.1",
|
||||||
"tsd-check": "^0.5.0",
|
"tsd": "^0.7.2",
|
||||||
"xo": "^0.24.0"
|
"xo": "^0.24.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue