piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 99 forks source link

The result type of createReducer is any instead of Reducer #256

Closed Eljoy closed 2 years ago

Eljoy commented 3 years ago

The result type of createReducer is any instead of Reducer

"typescript": "^4.2.4", "typesafe-actions": "^5.1.0",

Actions:

export const signIn = createAsyncAction(
  '[AUTH] SIGN_IN_REQUEST',
  '[AUTH] SIGN_IN_SUCCESS',
  '[AUTH] SIGN_IN_FAILURE'
)<void, Entities.Token, Error>()

export const signOut = createAsyncAction(
  '[AUTH] SIGN_OUT_REQUEST',
  '[AUTH] SIGN_OUT_SUCCESS',
  '[AUTH] SIGN_OUT_FAILURE'
)<void, void, Error>()

Reducer

// authReducer type = any
const authReducer = createReducer<StateType>(initialState)
  .handleAction(signIn.success, () => ({ isSignedIn: true }))
  .handleAction(signOut.success, () => ({ isSignedIn: false }))

Temp solution: Setting type explicitly:

const authReducer: Reducer<StateType, RootStateOrAny> = createReducer<StateType>(initialState)
  .handleAction(signIn.success, () => ({ isSignedIn: true }))
  .handleAction(signOut.success, () => ({ isSignedIn: false }))
zhang-quan-yi commented 2 years ago

I think RootAction may help: image

And this Todo-App is a complete demo: image

piotrwitek commented 2 years ago

Seems like resolved