Skip to content

Commit

Permalink
Merge pull request #1 from rikuayanokozy/master
Browse files Browse the repository at this point in the history
add typescript definitions
  • Loading branch information
Ehesp committed Nov 14, 2018
2 parents f74f87b + d26b4c9 commit 7b61d65
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Expand Up @@ -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<number|string>(Promise.resolve('123'));

// => Promise<[any, Array<number|string>]>
a2a<number|string>([Promise.resolve('123')]);

// => Promise<[Error, number|string>>
a2a<number|string, Error>(Promise.resolve('123'));
```
7 changes: 7 additions & 0 deletions index.d.ts
@@ -0,0 +1,7 @@
type Tuple<T1, T2> = [T1, T2];
type A2AResult<TResult, TError> = Tuple<TError|null, TResult|undefined>;

declare function a2a<TResult=any, TError = any>(promise: Promise<TResult>): Promise<A2AResult<TResult, TError>>;
declare function a2a<TResult=any, TError = any>(promise: Promise<TResult>[]): Promise<A2AResult<TResult[], TError>>;

export default a2a;
8 changes: 6 additions & 2 deletions package.json
Expand Up @@ -3,8 +3,9 @@
"version": "0.1.0",
"description": "Async await to Array",
"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",
Expand All @@ -23,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"
}
}
11 changes: 11 additions & 0 deletions 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[]);
});
12 changes: 12 additions & 0 deletions 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"]
}

0 comments on commit 7b61d65

Please sign in to comment.