-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
createAsyncThunk return fulfilled/rejected action instead of re-… (#361)
* createAsyncThunk return fulfilled/rejected action instead of re-trowing errors * add unwrapResult helper
- Loading branch information
Showing
3 changed files
with
81 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,56 @@ | ||
import { createAsyncThunk, Dispatch, createReducer } from 'src' | ||
import { createAsyncThunk, Dispatch, createReducer, AnyAction } from 'src' | ||
import { ThunkDispatch } from 'redux-thunk' | ||
import { promises } from 'fs' | ||
|
||
function expectType<T>(t: T) { | ||
return t | ||
} | ||
function fn() {} | ||
|
||
// basic usage | ||
{ | ||
const dispatch = fn as ThunkDispatch<any, any, any> | ||
;(async function() { | ||
const dispatch = fn as ThunkDispatch<{}, any, AnyAction> | ||
|
||
const async = createAsyncThunk('test', (id: number) => | ||
Promise.resolve(id * 2) | ||
) | ||
dispatch(async(3)) | ||
|
||
const reducer = createReducer({}, builder => | ||
builder | ||
.addCase(async.pending, (_, action) => {}) | ||
.addCase(async.pending, (_, action) => { | ||
expectType<ReturnType<typeof async['pending']>>(action) | ||
}) | ||
.addCase(async.fulfilled, (_, action) => { | ||
expectType<ReturnType<typeof async['fulfilled']>>(action) | ||
expectType<number>(action.payload) | ||
}) | ||
.addCase(async.rejected, (_, action) => {}) | ||
.addCase(async.rejected, (_, action) => { | ||
expectType<ReturnType<typeof async['rejected']>>(action) | ||
expectType<Error>(action.error) | ||
}) | ||
) | ||
} | ||
|
||
const promise = dispatch(async(3)) | ||
const result = await promise | ||
|
||
if (async.fulfilled.match(result)) { | ||
expectType<ReturnType<typeof async['fulfilled']>>(result) | ||
// typings:expect-error | ||
expectType<ReturnType<typeof async['rejected']>>(result) | ||
} else { | ||
expectType<ReturnType<typeof async['rejected']>>(result) | ||
// typings:expect-error | ||
expectType<ReturnType<typeof async['fulfilled']>>(result) | ||
} | ||
|
||
promise | ||
.then(async.unwrapResult) | ||
.then(result => { | ||
expectType<number>(result) | ||
// typings:expect-error | ||
expectType<Error>(result) | ||
}) | ||
.catch(error => { | ||
// catch is always any-typed, nothing we can do here | ||
}) | ||
})() |