Skip to content

Commit

Permalink
#2385: fixed put/putResolve typings for thunk actions (#2386)
Browse files Browse the repository at this point in the history
  • Loading branch information
qkudev committed Aug 26, 2023
1 parent 9c2af0c commit 9c59ac9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-trainers-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@redux-saga/core': patch
---

Fixes `put`/`putResolve` typings to support thunk actions
20 changes: 20 additions & 0 deletions packages/core/types/effects.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,20 @@ export type HelperWorkerParameters<T, Fn extends (...args: any[]) => any> = Last
? AllButLast<Parameters<Fn>>
: Parameters<Fn>

interface ThunkDispatch<State, ExtraThunkArg, BasicAction extends Action> {
<ReturnType>(thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>): ReturnType
<Action extends BasicAction>(action: Action): Action
<ReturnType, Action extends BasicAction>(
action: Action | ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>,
): Action | ReturnType
}

export type ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction extends Action> = (
dispatch: ThunkDispatch<State, ExtraThunkArg, BasicAction>,
getState: () => State,
extraArgument: ExtraThunkArg,
) => ReturnType

/**
* Creates an Effect description that instructs the middleware to dispatch an
* action to the Store. This effect is non-blocking, any errors that are
Expand All @@ -365,6 +379,9 @@ export type HelperWorkerParameters<T, Fn extends (...args: any[]) => any> = Last
* @param action [see Redux `dispatch` documentation for complete info](https://redux.js.org/api/store#dispatchaction)
*/
export function put<A extends Action>(action: A): PutEffect<A>
export function put<ReturnType = any, State = any, ExtraThunkArg = any, BasicAction extends Action = Action>(
action: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>,
): PutEffect<BasicAction>

/**
* Just like `put` but the effect is blocking (if promise is returned from
Expand All @@ -374,6 +391,9 @@ export function put<A extends Action>(action: A): PutEffect<A>
* @param action [see Redux `dispatch` documentation for complete info](https://redux.js.org/api/store#dispatchaction)
*/
export function putResolve<A extends Action>(action: A): PutEffect<A>
export function putResolve<ReturnType = any, State = any, ExtraThunkArg = any, BasicAction extends Action = Action>(
action: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>,
): PutEffect<BasicAction>

export type PutEffect<A extends Action = AnyAction> = SimpleEffect<'PUT', PutEffectDescriptor<A>>

Expand Down
10 changes: 10 additions & 0 deletions packages/core/types/effects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from 'redux-saga/effects'
import { Action, ActionCreator } from 'redux'
import { StringableActionCreator, ActionMatchingPattern } from '@redux-saga/types'
import { ThunkAction } from '@redux-saga/core/effects'

interface MyAction extends Action {
customField: string
Expand Down Expand Up @@ -92,7 +93,15 @@ function* testTake(): SagaIterator {
}

function* testPut(): SagaIterator {
type TestThunkAction = ThunkAction<number, object, object, { type: 'thunk-action' }>
const thunkActionCreator = (): TestThunkAction => (dispatch) => {
dispatch({ type: 'thunk-action' })

return 42
}

yield put({ type: 'my-action' })
yield put(thunkActionCreator())

// $ExpectError
yield put(channel, { type: 'my-action' })
Expand All @@ -109,6 +118,7 @@ function* testPut(): SagaIterator {
yield put(multicastChannel, END)

yield putResolve({ type: 'my-action' })
yield putResolve(thunkActionCreator())
}

function* testCall(): SagaIterator {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/types/ts3.6/effects.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,20 @@ export type HelperWorkerParameters<T, Fn extends (...args: any[]) => any> = Last
? AllButLast<Parameters<Fn>>
: Parameters<Fn>

interface ThunkDispatch<State, ExtraThunkArg, BasicAction extends Action> {
<ReturnType>(thunkAction: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>): ReturnType
<Action extends BasicAction>(action: Action): Action
<ReturnType, Action extends BasicAction>(
action: Action | ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>,
): Action | ReturnType
}

export type ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction extends Action> = (
dispatch: ThunkDispatch<State, ExtraThunkArg, BasicAction>,
getState: () => State,
extraArgument: ExtraThunkArg,
) => ReturnType

/**
* Creates an Effect description that instructs the middleware to dispatch an
* action to the Store. This effect is non-blocking, any errors that are
Expand All @@ -384,6 +398,9 @@ export type HelperWorkerParameters<T, Fn extends (...args: any[]) => any> = Last
* @param action [see Redux `dispatch` documentation for complete info](https://redux.js.org/api/store#dispatchaction)
*/
export function put<A extends Action>(action: A): PutEffect<A>
export function put<ReturnType = any, State = any, ExtraThunkArg = any, BasicAction extends Action = Action>(
action: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>,
): PutEffect<BasicAction>

/**
* Just like `put` but the effect is blocking (if promise is returned from
Expand All @@ -393,6 +410,9 @@ export function put<A extends Action>(action: A): PutEffect<A>
* @param action [see Redux `dispatch` documentation for complete info](https://redux.js.org/api/store#dispatchaction)
*/
export function putResolve<A extends Action>(action: A): PutEffect<A>
export function putResolve<ReturnType = any, State = any, ExtraThunkArg = any, BasicAction extends Action = Action>(
action: ThunkAction<ReturnType, State, ExtraThunkArg, BasicAction>,
): PutEffect<BasicAction>

export type PutEffect<A extends Action = AnyAction> = SimpleEffect<'PUT', PutEffectDescriptor<A>>

Expand Down

0 comments on commit 9c59ac9

Please sign in to comment.