From aa06166a0ebe0f6edd8b18a31f03d787930478ac Mon Sep 17 00:00:00 2001 From: Riku Date: Sat, 10 Nov 2018 01:42:00 +0800 Subject: [PATCH 1/3] add typescript definitions --- index.d.ts | 7 +++++++ package.json | 1 + 2 files changed, 8 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..70e98fc --- /dev/null +++ b/index.d.ts @@ -0,0 +1,7 @@ +type Tuple = [T1, T2]; +type A2AResult = Tuple; + +declare function a2a(promise: Promise): Promise>; +declare function a2a(promise: Promise[]): Promise>; + +export default a2a; diff --git a/package.json b/package.json index d1a10f8..6d9e71f 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "description": "Async await to Array", "main": "index.js", + "types": "index.d.ts", "scripts": { "test": "nyc --reporter=html mocha" }, From ff9483b07db8252683393476c42907e19789c838 Mon Sep 17 00:00:00 2001 From: Riku Date: Sat, 10 Nov 2018 01:52:26 +0800 Subject: [PATCH 2/3] add types test --- package.json | 7 +++++-- test/tsd.ts | 11 +++++++++++ tsconfig.json | 12 ++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test/tsd.ts create mode 100644 tsconfig.json diff --git a/package.json b/package.json index 6d9e71f..08db1de 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "types": "index.d.ts", "scripts": { - "test": "nyc --reporter=html mocha" + "test": "nyc --reporter=html mocha && ts-node test/tsd.ts" }, "repository": { "type": "git", @@ -24,7 +24,10 @@ }, "homepage": "https://github.com/invertase/a2a#readme", "devDependencies": { + "@types/node": "^10.12.4", "mocha": "^5.2.0", - "nyc": "^13.0.1" + "nyc": "^13.0.1", + "ts-node": "^7.0.1", + "typescript": "^3.1.6" } } diff --git a/test/tsd.ts b/test/tsd.ts new file mode 100644 index 0000000..5e9fa78 --- /dev/null +++ b/test/tsd.ts @@ -0,0 +1,11 @@ +import a2a from '../'; + +a2a(Promise.resolve(1)) + .then(([err, result]) => { + console.log(result as number); + }); + +a2a([Promise.resolve(1)]) + .then(([err, result]) => { + console.log(result as number[]); + }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f625847 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "commonjs", + "lib": ["es2015"], + "strict": true, + "moduleResolution": "node", + "types": ["node"], + "esModuleInterop": true + }, + "include": ["index.d.ts", "test/*.ts"] +} \ No newline at end of file From d26b4c98c80f1bed00b290e76c6c25a7a7da799e Mon Sep 17 00:00:00 2001 From: Riku Date: Sat, 10 Nov 2018 01:59:10 +0800 Subject: [PATCH 3/3] Update README.md for typescript --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index 97fdc21..73df1dd 100644 --- a/README.md +++ b/README.md @@ -44,3 +44,22 @@ async function example2() { example1(); example2(); ``` + +## TypeScript +```ts +import a2a from 'a2a'; + +a2a(Promise.resolve(123)) // => Promise<[any, number]> + .then(([error, result]) => { + console.log(result as number); + }); + +// => Promise<[any, number|string]> +a2a(Promise.resolve('123')); + +// => Promise<[any, Array]> +a2a([Promise.resolve('123')]); + +// => Promise<[Error, number|string>> +a2a(Promise.resolve('123')); +```