-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug?]: Yarn 2 PnP causes incorrect Typescript typings with redux-toolkit #3150
Comments
I was able to get this working in the test project by upgrading the However, I have another project where that did NOT fix the problem. That project uses workspaces. So I converted the test project to use workspaces, and the problem reappears: https://github.com/joekrill/react-redux-thunk-typescript-redux-toolkit-starter-kit/tree/monorep. BUT! - if I explicitly add So I think this has something to do with typescript not loading the types for And to be thorough, here are some additional things I've also tried with no success:
|
Generally when this happen it's because the type package doesn't declare its dependency on whatever package it depends on for its type (even if its runtime doesn't depend on it itself). TypeScript then silently fallbacks to To debug that, I'd recommend to use ctrl+click to jump into the redux toolkit types, then see what are the imports in this file. Once you find the one that becomes |
The issue is that packageExtensions:
'redux-thunk@*':
peerDependencies:
redux: '*' Interestingly it was fixed two years ago but it hasn't been released it yet, I'll merge it into our builtin The second error is that you're missing types for yarn add @types/testing-library__react@^9.0.0 The way I debugged this was to run PnP in debug mode so it prints out all rejected resolutions PNP_DEBUG_LEVEL=1 yarn tsc That gives a lot of output but the important part is
See https://yarnpkg.com/features/pnp#fallback-mode
It works with |
Thanks so much @arcanis and @merceyz -- you're absolutely right. Oddly, I had tried fixing any dependency problems using ➤ YN0002: │ typescript-react-redux-redux-saga-starter-kit_version_2@workspace:. doesn't provide redux (p60dc0), requested by react-redux I wonder why it didn't flag the In any case, I really appreciate the help and additional information - I've spent quite a bit of time trying to track down the problem or find a workaround! |
That check is for |
For any poor souls finding this thread from Google and not being able to understand why they are still getting errors after the issue should seemingly be resolved, in my case the issue was with the definition of the configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) => [...getDefaultMiddleware({ thunk: { extraArgument } })]
}); which destroys the proper return type provided by If all you need is the defaults, then simply remove the spread syntax: configureStore({
reducer: rootReducer,
middleware: (getDefaultMiddleware) => getDefaultMiddleware({ thunk: { extraArgument } })
}); This cleared up the type issues for me. |
Self-service
Describe the bug
Upgrading to Yarn 2 with PnP in a typescript project that uses
redux-toolkit
causes the type thatconfigureStore
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-typeUltimately, the code is something like this:
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 aThunkDispatch
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
The text was updated successfully, but these errors were encountered: