redux-devtools icon indicating copy to clipboard operation
redux-devtools copied to clipboard

TS2344: Type 'unknown' does not satisfy the constraint '{}'

Open Philipp91 opened this issue 1 year ago • 6 comments

Using:

"redux": "^5.0.1",
"@redux-devtools/extension": "^3.3.0",

I get:

node_modules/@redux-devtools/extension/lib/types/index.d.ts:227:70 - error TS2344: Type 'unknown' does not satisfy the constraint '{}'.

227     (config: Config): <StoreEnhancers extends readonly StoreEnhancer<unknown>[]>(...funcs: StoreEnhancers) => StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
~~~~~~~

node_modules/@redux-devtools/extension/lib/types/index.d.ts:227:125 - error TS2344: Type 'InferComposedStoreExt<StoreEnhancers>' does not satisfy the constraint '{}'.
Type 'unknown' is not assignable to type '{}'.
Type 'unknown' is not assignable to type '{}'.

227     (config: Config): <StoreEnhancers extends readonly StoreEnhancer<unknown>[]>(...funcs: StoreEnhancers) => StoreEnhancer<InferComposedStoreExt<StoreEnhancers>>;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Maybe this was broken by https://github.com/reduxjs/redux/commit/5a084a62fda9ebf39ffa969458d9950b4f051688:

- export type StoreEnhancer<Ext = {}, StateExt = never>
+ export type StoreEnhancer<Ext extends {} = {}, StateExt extends {} = {}>

Philipp91 avatar Jan 06 '24 21:01 Philipp91

I've updated the bug to reflect the error that this (new) extension gives me.

Philipp91 avatar Jan 06 '24 21:01 Philipp91

Workaround (or solution?):

After installing the library, in node_modules/@redux-devtools/extension/lib/types/index.d.ts and node_modules/@redux-devtools/extension/lib/types/logOnly.d.ts, change all occurrences of StoreEnhancer<...> to StoreEnhancer<... & {}>.

Philipp91 avatar Jan 06 '24 21:01 Philipp91

Note that you shouldn't be using either the core redux package or @redux-devtools/extension directly in your own code today. Instead, use Redux Toolkit's configureStore, which sets that up for you automatically:

  • https://redux.js.org/usage/migrating-to-modern-redux#store-setup-with-configurestore

markerikson avatar Jan 06 '24 23:01 markerikson

Right. Still.

I think this TypeScript error happens irrespective of what APIs I call, and just because of the mere existence of node_modules/@redux-devtools/extension/lib/types/index.d.ts in my TypeScript inputs -- and that wouldn't go away with the Toolkit.

(I'm lazy about such a migration... Despite the claim "This is a one-time step, and all of the existing reducers and middleware will continue to work as-is." I ran into TypeScript errors last time and decided to postpone this.)

Philipp91 avatar Jan 07 '24 12:01 Philipp91

@Philipp91 fwiw, that should go away with RTK and configureStore, because RTK does not depend on @redux-devtools/extension itself - it inlines some of the types. So yeah, migrating the store setup to RTK would be one way to fix this.

I'd also recommend checking to see if you somehow have multiple copies of redux in your node_modules.

markerikson avatar Jan 07 '24 16:01 markerikson

Okay thanks, I've managed to migrate with reasonable effort and I can confirm that the issue doesn't show up there. As you suspected, it doesn't install the redux-devtools module at all then.

(No multiple copies. I believe the error comes from a misalignment of redux-devtools code with the updated redux code I linked in the opening post.)

Philipp91 avatar Jan 07 '24 18:01 Philipp91