Skip to content

Commit eb30116

Browse files
authored
Use union for AsyncState type
1 parent f856918 commit eb30116

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/index.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,31 @@ export type AsyncStateStatus =
4343
| 'success'
4444
| 'error';
4545

46-
export type AsyncState<R> = {
47-
status: AsyncStateStatus;
48-
loading: boolean;
49-
error: Error | undefined;
50-
result: R | undefined;
51-
};
46+
export declare type AsyncState<R> =
47+
| {
48+
status: 'not-requested';
49+
loading: false;
50+
result: undefined;
51+
error: undefined;
52+
}
53+
| {
54+
status: 'loading';
55+
loading: true;
56+
error: undefined;
57+
result: undefined;
58+
}
59+
| {
60+
status: 'success';
61+
loading: false;
62+
error: undefined;
63+
result: R;
64+
}
65+
| {
66+
status: 'error';
67+
loading: false;
68+
error: Error;
69+
result: undefined;
70+
};
5271
type SetLoading<R> = (asyncState: AsyncState<R>) => AsyncState<R>;
5372
type SetResult<R> = (result: R, asyncState: AsyncState<R>) => AsyncState<R>;
5473
type SetError<R> = (error: Error, asyncState: AsyncState<R>) => AsyncState<R>;

0 commit comments

Comments
 (0)