-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Self-service
- I'd be willing to implement a fix
Describe the bug
Upgrading to Yarn 2 with PnP in a typescript project that uses redux-toolkit
causes the type that configureStore
generates to be incorrect.
When using redux-toolkit
with typescript, the documentation describes how to type the application Dispatch type: https://redux-toolkit.js.org/usage/usage-with-typescript#getting-the-dispatch-type
Ultimately, the code is something like this:
import { configureStore } from '@reduxjs/toolkit';
import { useDispatch } from 'react-redux';
import rootReducer from './rootReducer';
const store = configureStore({ reducer: rootReducer });
export type AppDispatch = typeof store.dispatch;
export const useAppDispatch = () => useDispatch<AppDispatch>();
export default store;
Which results in the AppDispatch
being typed in a specific way. In particular, it is typed so that it knows it can accept "thunks". It ends up looking something like this (with the key part being that it includes a ThunkDispatch
typing):
After upgrading to Yarn 2 and enabling PnP, the types are significantly different and incorrect, with the ThunkDispatch type missing (among other things):
This isn't just limited to VSCode -- running a typecheck from the command line (yarn run tsc --noEmit
) produces errors caused by this incorrect typing.
I really don't know if this is a bug in Yarn, or perhaps an issue with how Yarn 2 patches typescript, or maybe it's even a typing issue with redux-toolkit
(or a dependency). But I'm able to reproduce the issue using a working repo using Yarn 1, upgrading it to Yarn 2, and enabling PnP. Everything works as expected until the point of switching to PnP. I'm not even sure how to debug this further, but so far what I've gathered points to it being an issue with PnP. I was hoping someone here might have some additional insight.
To reproduce
To reproduce, I started with this basic "starter-kit" repo that uses Yarn 1: https://github.com/davidjamescarter/react-redux-thunk-typescript-redux-toolkit-starter-kit
I forked that and migrated it to Yarn 2: https://github.com/joekrill/react-redux-thunk-typescript-redux-toolkit-starter-kit
There are 2 additional commits: the first migrates to Yarn 2 without PnP. The final commit enabled PnP. And that's when the typing problems occur.
Environment
System:
OS: macOS 11.4
CPU: (8) x64 Apple M1
Binaries:
Node: 14.16.0 - /private/var/folders/g0/j5y10l9s7dq8q2qqm9r92xl00000gn/T/xfs-44942072/node
Yarn: 2.4.2 - /private/var/folders/g0/j5y10l9s7dq8q2qqm9r92xl00000gn/T/xfs-44942072/yarn
npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
Additional context
No response