question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider removing `DefaultRootState` type

See original GitHub issue

React-Redux v8 currently has a DefaultRootState type defined:

https://github.com/reduxjs/react-redux/blob/4d384029e3fa0355e762f40409a5dac4358a3de3/src/types.ts#L13-L23

This was copy-pasted directly from the @types/react-redux types in DefinitelyTyped, which have had that for a while.

We Redux maintainers were not involved in that type being added originally, and specifically think it’s a bad practice:

Or to say it bluntly: If at that point in time, we as Redux maintainers would have known about what was going on in DefinitelyTyped, we would have prevented DefaultRootState from ever being added. Unfortunately, at that point we were not aware.

It is a global enhancement of the state, no matter if the reducer was actually added to your store.

At one point in time, some library will go ahead and pollute that interface and suddenly every project using that library will have an extra property in their Redux store - without any reducer for it being present.

There is a good reason we don’t recommend doing this anywhere in the docs.

Given that, I’d like to propose removing it from the v8 typings before this gets released.

I’m assuming we’d want to replace that with unknown instead.

I did a couple code searches, and I see maybe 1000-1500 usages in the wild:

So that’s not zero, but it isn’t a thing that would completely break the ecosystem in the upgrade process.

Lenz also said that this is a pattern that could get replicated in userspace if desired, although I’m not sure what the details are on that.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
adamaveraycommented, Apr 21, 2022

In case anyone is looking to replicate augmenting the DefaultRootState type (to avoid having to explicitly declare the state’s type in every call to useSelector()) the following module augmentation file should work:

// react-redux.d.ts
import type { EqualityFn } from 'react-redux/es/types';
import type { GlobalState } from '...'; // Your root store type

declare module 'react-redux' {
  export declare const useSelector: <T>(selector: (state: GlobalState) => T, equalityFn?: EqualityFn<T>) => T;
}

(The file should be placed in a directory listed in the project’s tsconfig.json’s typeRoots array – see the docs)

Now calls such as useSelector(state => ...) will automatically treat the state parameter as having the above GlobalState type.

2reactions
markeriksoncommented, Apr 10, 2022

Just did this in #1887 . There was also already a Connect<State = DefaultRootState> type being exported, so that just defaults to unknown now.

(That Connect type could theoretically be modified to accept a Dispatch generic too, but it looks like it would be complicated to fit in and frankly I don’t want to worry about it.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Redux Connect DefaultRootState typing - Stack Overflow
The last template parameter of connect is RootState , the type of the root state. So the solution was to correctly parametrize the...
Read more >
Usage with TypeScript - React Redux
The recently updated @types/react@18 major version has changed component definitions to remove having children as a prop by default.
Read more >
Mark Erikson on Twitter: "Request for feedback: React-Redux v8 ...
Consider removing `DefaultRootState` type · Issue #1879 · reduxjs/react-redux. React-Redux v8 currently has a DefaultRootState type defined: ...
Read more >
How to add Redux to a React app (TypeScript) - YouTube
... 11:43 useSelector 14:28 useDispatch 17:00 DefaultRootState type error 18:20 Custom useAppSelector hook 21:19 Custom useAppDispatch hook ...
Read more >
Global state and action interfaces - Tabris.js Documentation
declare module 'tabris-decorators' { export interface DefaultRootState { // add state ... ReducersMapObject<DefaultRootState, AnyAction>} Reducers * @type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found