Skip to content

Commit a670f23

Browse files
authored
Merge pull request #12 from ghengeveld/typings
Add type definitions for TypeScript (fixes #10)
2 parents f90805a + 4e8ffa2 commit a670f23

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
"registry": "https://registry.npmjs.org/"
2020
},
2121
"main": "lib/index.js",
22+
"typings": "typings/index.d.ts",
2223
"files": [
2324
"lib",
24-
"src"
25+
"src",
26+
"typings"
2527
],
2628
"scripts": {
2729
"build": "babel src -d lib",

typings/index.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from "react"
2+
3+
type AsyncChildren<T> = ((state: AsyncState<T>) => React.ReactNode) | React.ReactNode
4+
5+
interface AsyncProps<T> {
6+
promiseFn?: (props: object) => Promise<T>
7+
deferFn?: (...args) => Promise<T>
8+
watch?: any
9+
initialValue?: T
10+
onResolve?: (data: T) => void
11+
onError?: (error: Error) => void
12+
children?: AsyncChildren<T>
13+
}
14+
15+
interface AsyncState<T> {
16+
initialValue?: T
17+
data?: T
18+
error?: Error
19+
isLoading: boolean
20+
startedAt?: Date
21+
finishedAt?: Date
22+
cancel: () => void
23+
run: (...args) => Promise<T>
24+
reload: () => void
25+
setData: (data: T, callback?: () => void) => T
26+
setError: (error: Error, callback?: () => void) => Error
27+
}
28+
29+
declare class Async<T> extends React.Component<AsyncProps<T>, AsyncState<T>> {
30+
static Pending: React.FunctionComponent<{ children?: AsyncChildren<T>; persist?: boolean }>
31+
static Loading: React.FunctionComponent<{ children?: AsyncChildren<T>; initial?: boolean }>
32+
static Resolved: React.FunctionComponent<{ children?: AsyncChildren<T>; persist?: boolean }>
33+
static Rejected: React.FunctionComponent<{ children?: AsyncChildren<T>; persist?: boolean }>
34+
}
35+
36+
declare function createInstance<T>(defaultProps?: AsyncProps<T>): Async<T>
37+
38+
export default createInstance

0 commit comments

Comments
 (0)