combineReducers doesn't infer type

前端 未结 2 1892
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 05:27

I am using combineReducersin my React TypeScript app:

// combinedReducer.ts
import { combineReducers } from \'redux\'
import reducer1 from \'./r         


        
2条回答
  •  礼貌的吻别
    2021-01-15 06:06

    Same problem for me using redux 4.0.5 and typescript 4.0.1. Had to write a inferrer myself:

    type BaseReducerMap = {
        [K in keyof S]: (state: S[K], action: any) => S
    }
    
    export type InferRootState, S = any> = {
        [K in keyof ReducerMap]: ReturnType
    }
    

    Then for your set of reducers (before you combine them)

    const reducers = {
        login: loginReducer,
        chatUsers: chatUsersReducer,
    };
    const combinedReducer = combineReducers(reducers);
    
    export type State = InferRootState;
    

    Then typescript can successfully infer the type of my State. I don't know why it fails in this version.

提交回复
热议问题