Skip to content

Commit 907d226

Browse files
committed
Add typings for useAsync.
1 parent a15adbd commit 907d226

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/useAsync.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ const useAsync = (opts, init) => {
8181
}
8282

8383
const unsupported = () => {
84-
throw new Error("useAsync requires [email protected] or later")
84+
throw new Error(
85+
"useAsync requires [email protected] or later. Upgrade your React version or use the <Async> component instead."
86+
)
8587
}
8688

8789
export default (useState ? useAsync : unsupported)

typings/index.d.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import * as React from "react"
22

33
type AsyncChildren<T> = ((state: AsyncState<T>) => React.ReactNode) | React.ReactNode
4+
type PromiseFn<T> = (props: object) => Promise<T>
45

5-
interface AsyncProps<T> {
6-
promiseFn?: (props: object) => Promise<T>
6+
interface AsyncOptions<T> {
7+
promiseFn?: PromiseFn<T>
78
deferFn?: (...args) => Promise<T>
89
watch?: any
910
initialValue?: T
1011
onResolve?: (data: T) => void
1112
onError?: (error: Error) => void
13+
}
14+
15+
interface AsyncProps<T> extends AsyncOptions<T> {
1216
children?: AsyncChildren<T>
1317
}
1418

@@ -37,4 +41,6 @@ declare namespace Async {
3741

3842
declare function createInstance<T>(defaultProps?: AsyncProps<T>): Async<T>
3943

44+
export function useAsync<T>(opts: AsyncOptions<T> | PromiseFn<T>, init?: T): AsyncState<T>
45+
4046
export default createInstance

0 commit comments

Comments
 (0)