Browse Source

Add TypeScript definition (#3)

master
Sam Verschueren 6 years ago
committed by Sindre Sorhus
parent
commit
c01829e2cf
  1. 13
      index.d.ts
  2. 5
      index.js
  3. 11
      index.test-d.ts
  4. 6
      package.json

13
index.d.ts

@ -0,0 +1,13 @@
/**
* Check if `input` is a ES2015 promise.
*
* @param input - Value to be checked.
*
* @example
*
* import isPromise from 'p-is-promise';
*
* isPromise(Promise.resolve('🦄'));
* //=> true
*/
export default function(input: unknown): input is Promise<unknown>;

5
index.js

@ -1,5 +1,5 @@
'use strict';
module.exports = x => (
const isPromise = x => (
x instanceof Promise ||
(
x !== null &&
@ -8,3 +8,6 @@ module.exports = x => (
typeof x.catch === 'function'
)
);
module.exports = isPromise;
module.exports.default = isPromise;

11
index.test-d.ts

@ -0,0 +1,11 @@
import {expectType} from 'tsd-check';
import isPromise from '.';
expectType<boolean>(isPromise('🦄'));
const unicorn: unknown = Promise.resolve('🦄');
if (isPromise(unicorn)) {
expectType<Promise<unknown>>(unicorn);
expectType<unknown>(await unicorn);
}

6
package.json

@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd-check"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
@ -35,6 +36,7 @@
"devDependencies": {
"ava": "^0.25.0",
"bluebird": "^3.4.6",
"tsd-check": "^0.2.1",
"xo": "^0.23.0"
}
}

Loading…
Cancel
Save