Consider removing `DefaultRootState` type
See original GitHub issueReact-Redux v8 currently has a DefaultRootState
type defined:
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:
- https://sourcegraph.com/search?q=context:global+lang:typescript+DefaultRootState+count:2000&patternType=literal
- https://github.com/search?l=TypeScript&q=DefaultRootState&type=Code
- https://cs.github.com/?q=DefaultRootState
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:
- Created 2 years ago
- Reactions:4
- Comments:6 (4 by maintainers)
In case anyone is looking to replicate augmenting the
DefaultRootState
type (to avoid having to explicitly declare the state’s type in every call touseSelector()
) the following module augmentation file should work:(The file should be placed in a directory listed in the project’s
tsconfig.json
’stypeRoots
array – see the docs)Now calls such as
useSelector(state => ...)
will automatically treat thestate
parameter as having the aboveGlobalState
type.Just did this in #1887 . There was also already a
Connect<State = DefaultRootState>
type being exported, so that just defaults tounknown
now.(That
Connect
type could theoretically be modified to accept aDispatch
generic too, but it looks like it would be complicated to fit in and frankly I don’t want to worry about it.)