Skip to content

Commit 983f154

Browse files
authored
Merge pull request #7 from icopp/add-typescript-definitions
Add Typescript definitions
2 parents b94daa0 + b64d007 commit 983f154

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,13 @@ All props are optional
7070
- **then** runs when promise is resolved. Async will run function provided in it's render passing a resolved value as first parameter.
7171
- **catch** runs when promise is rejected. Async will run function provided in it's render passing an error as first parameter.
7272
- **pendingRender** is a node which will be outputted from Async render method while promise is pending. If none is provided, defaults to `<div/>`
73+
74+
## To use with Typescript
75+
76+
```
77+
import Async, { Props as AsyncProps } from 'react-promise'
78+
79+
const StringAsync = Async as { new (props: AsyncProps<string>): Async<string> }
80+
```
81+
82+
The type used for the generic will be matched against the type for the promise's value. This workaround is necessary because currently there's no way to directly supply generic types in Typescript.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.1.3",
44
"description": "react.js component for dealing with promises",
55
"main": "./lib/async.js",
6+
"types": "./lib/async.d.ts",
67
"jspm": {
78
"format": "esm"
89
},

src/async.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as React from 'react'
2+
3+
export interface Props<T> {
4+
promise?: Promise<T>
5+
before?: (handlePromise: () => void) => React.ReactNode
6+
then?: (value: T) => React.ReactNode
7+
catch?: (err: any) => React.ReactNode
8+
pendingRender?: React.ReactNode
9+
}
10+
11+
export interface State {
12+
started: boolean
13+
resolved: boolean
14+
finished: boolean
15+
rejected: boolean
16+
}
17+
18+
declare class Async<T> extends React.Component<Props<T>, State> { }
19+
20+
export default Async

0 commit comments

Comments
 (0)