Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get type of `rootReducer`?

I have a configured project with React, Redux, Immutable.js + TypeScript. During implementation I was trying to declare types as much as possible and found interesting issue. See code example below:

Short configuration of redux store

import { createStore } from 'redux';
import { combineReducers } from 'redux-immutable';

const rootReducer = combineReducers({...});
const store = createStore(rootReducer);

Somewhere inside component

// ...
const mapStateToProps = (state: ReturnType<typeof rootReducer>) => {
  // state is plain object :(
};

On state hover in VS Code, tooltip shows that state is a plain object, however it is not. It should be a custom collection from Immutable.js

How can I get a correct type of rootReducer? Or what I am doing wrong?

Screenshots:

Hovered mouse over state variable

Hovered mouse over emphasized get method

P.S. StateType and ReturnType do the same stuff

like image 755
Roman Mahotskyi Avatar asked Oct 20 '25 03:10

Roman Mahotskyi


1 Answers

This works (no need for Immutable.js):

export type RootState = ReturnType<typeof rootReducer>;

If for some reason it does not works, this one should work:

export type RootState = ReturnType<typeof store.getState>
like image 192
Gabriel Beauchemin-Dauphinais Avatar answered Oct 21 '25 18:10

Gabriel Beauchemin-Dauphinais