Skip to content

Commit 9c5c7dd

Browse files
committed
Update typings based on feedback from Tom Shane.
1 parent 8b602c5 commit 9c5c7dd

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

typings/index.d.ts

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,38 @@
11
import * as React from "react"
22

3-
type ChildrenFunction = (state: object) => React.ReactNode
3+
type AsyncChildren<T> = ((state: AsyncState<T>) => React.ReactNode) | React.ReactNode
44

5-
interface AsyncProps {
6-
children?: React.ReactNode | ChildrenFunction
7-
promiseFn?(props: object): Promise<any>
8-
deferFn?(props: object): Promise<any>
5+
interface AsyncProps<T> {
6+
promiseFn?: (props: object) => Promise<T>
7+
deferFn?: (...args, props: object) => Promise<T>
98
watch?: any
10-
initialValue?: any
11-
onResolve?(data: any): any
12-
onReject?(error: Error): any
9+
initialValue?: T
10+
onResolve?: (data: T) => void
11+
onError?: (error: Error) => void
12+
children?: AsyncChildren<T>
1313
}
1414

15-
interface PendingProps {
16-
children?: React.ReactNode | ChildrenFunction
17-
persist?: boolean
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
1827
}
1928

20-
interface LoadingProps {
21-
children?: React.ReactNode | ChildrenFunction
22-
initial?: boolean
29+
class Async<T> extends React.Component<AsyncProps<T>, AsyncState<T>> {
30+
static Pending: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }>
31+
static Loading: React.FunctionComponent<{ children?: AsyncChildren; initial?: boolean }>
32+
static Resolved: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }>
33+
static Rejected: React.FunctionComponent<{ children?: AsyncChildren; persist?: boolean }>
2334
}
2435

25-
interface ResolvedProps {
26-
children?: React.ReactNode | ChildrenFunction
27-
persist?: boolean
28-
}
29-
30-
interface RejectedProps {
31-
children?: React.ReactNode | ChildrenFunction
32-
persist?: boolean
33-
}
34-
35-
declare class Async extends React.Component<AsyncProps, any> {
36-
public static Pending: React.SFC<PendingProps>
37-
public static Loading: React.SFC<LoadingProps>
38-
public static Resolved: React.SFC<ResolvedProps>
39-
public static Rejected: React.SFC<RejectedProps>
40-
}
41-
42-
declare function createInstance(defaultProps?: AsyncProps): Async
36+
function createInstance<T>(defaultProps?: AsyncProps<T> = {}): Async<T>
4337

4438
export default createInstance

0 commit comments

Comments
 (0)