-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
The inferred type of 'X' cannot be named without a reference to '@reduxjs/toolkit/node_modules/immer/dist/internal'. This is likely not portable. A type annotation is necessary. #1806
Comments
There was one prior report of this in #1181 . Unfortunately, this really sounds like some kind of a bundling/installation problem, and not something that's actually wrong with the source code. Given that thousands of people are using RTK with TypeScript without issues, this is probably something very specific to your project setup, and not anything we can investigate and fix ourselves. If you can manage to put together a project that demonstrates this happening we might be able to take a look, but otherwise I suspect there's nothing we can do here. |
I'm quite inexperienced in TypeScript. Can you maybe just point me in a direction where I should start troubleshooting? |
I solved the error by doing the following in my tsconfig.json This is probably just a workaround but if someone comes across this problem this should work. {
"compilerOptions": {
"declaration": false,
},
} |
@filiptrplan thanks |
TLDR; No clue why this happens, but you can add Follow up on this one. I'm working with two different monorepos, both using RTK, and both using Here's the deal for me. One project has this error (let's call it project CRAP), the other project doesn't (call this one CLAP). I have desperately tried to figure out why this error is occurring in CRAP and not in CLAP. Nearly everything I can think of I have normalized between the two:
And when I say the versions are the same, these are both running yarn workspaces, properly deduped. So I do mean the same. The only thing that's different, that I can see at least, is the project structure and the path mappings in the tsconfigs. In CLAP, the apps themselves are not part of the path mappings (although, everything is still defined in workspaces). Only the "shared" and externally published packages are defined in the path mappings. I frankly can't see how that would matter though. I also tried, changing the path mappings in CRAP so only the "shared" modules are mapped, and this did nothing. All of this said, long before I tried any of this, I did find a way to "trick" TypeScript into thinking all is right in the world by adding If I'm able to turn CRAP into CLAP, then I'll update this. |
@ctbwx I have no idea what you're trying to say here, but explicitly typing Maybe you explain how this relates to the issue here, at least. |
For me, it only works if I use version |
only need to declare the slice explicitly to resolve this ts type error |
@PeiLiao : please don't do that :( You're throwing away all the actual TS types in |
Since google brought me to this one first, linking to my comment confirming a fix in the referenced issue: #1181 (comment) |
Ran into this issue too - fiddling around with Immer didn't work for me, but it resolved itself once all applications in my monorepo were on the exact same (newest) version of redux-toolkit. |
Hey the solution is to:
We could fix this inside |
Adding A simple |
In my case, unfortunately, none of these setups is working. Attempts:
The only setup works when I disable |
A solution might be adding |
As another potential not the prettiest workaround. |
Thank you @srosato this worked! |
I got the same error while trying the test
The issue was that yarn does package hoisting ending up with conflicting Moving all versions to |
For me, none of the above works. Edit: Installing the same |
I also recently switched to What I did:
Then the error went away. No need to my earlier workaround I posted earlier here in this thread when using |
Actually, I am not sure if using /* eslint-disable-next-line @typescript-eslint/no-unused-vars,unused-imports/no-unused-imports */ // noinspection ES6UnusedImports
import type { Draft } from 'immer' The error also goes away like that on |
Importing immer didn't work for me, so I referenced it as
|
I came across this same error when using export interface POSState {
addedPOSItems: POSItem[],
}
const initialState = {
addedPOSItems: []
} as POSState;
export const POSSlice = createSlice({
name: 'pos',
initialState,
reducers: {
// Change `state` to `state: POSState`
// vvvvvvvvvvvvvvv
setAddedPOSItems: (state: POSState, action: PayloadAction<POSItem[]>) => {
state.addedPOSItems = action.payload;
}
}
}); In my case, I had code that looks like this: const fooAdapter = createEntityAdapter<FooModel>();
export const fooSlice = createSlice({
name: "foo",
initialState: fooAdapter.getInitialState({
myValue: 0
}),
reducers: {
addFoo: FooAdapter.addOne,
setFoo: FooAdapter.setOne,
updateFoo: FooAdapter.updateOne,
removeFoo: FooAdapter.removeOne,
// Adding this reducer causes the error
setValue: (state, action: PayloadAction<number>) => {
state.myValue = action.payload;
}
},
}); I fixed the error by adding an interface that declared the shape of the state object returned by export interface FooState extends EntityState<FooModel> {
myValue: number;
}
const fooAdapter = createEntityAdapter<FooModel>();
export const fooSlice = createSlice({
name: "foo",
initialState: fooAdapter.getInitialState({
myValue: 0
}),
reducers: {
addFoo: FooAdapter.addOne,
setFoo: FooAdapter.setOne,
updateFoo: FooAdapter.updateOne,
removeFoo: FooAdapter.removeOne,
setValue: (state: FooState, action: PayloadAction<number>) => {
state.myValue = action.payload;
}
},
}); |
This is how I solved it: Then in ther xxx.slice.ts files top add
|
@sallyzoughaib that seems to be completely unrelated to this project - and honestly, it's a very weird way of installing your dependencies. Even if runtime JavaScript might pick them up, TypeScript probably doesn't. |
For those for whom importing "immer" or turning "declaration" off did not work/is not an option: If this happens for a slice: instead of exporting the slice directly, only export the specific attributes that are used to register the slice in the store: const navigationSlicePrivate = createSlice({
name: 'navigation',
initialState,
reducers: {
/*....*/
}
}
export const navigationSlice = {
name: navigationSlicePrivate.name,
reducer: navigationSlicePrivate.reducer
} In our application, we have around 25 slices, and only a single one of those has this problem. Comparing the slices, there seem to be no obvious difference. |
The fix for me was to add declaring the slice as
|
@shailesh17 this isn't really a solution as you lose all specific information about the slice - what actions it has, its name, etc |
this problem is described here https://typescript.tv/errors/#ts2742 |
We see this issue a lot using rtk in our monorepo and solve it in the way noted here, i.e., an import of immer as a dev dependency. However, I do think the error is actually a typing error (albeit a minor one) namely that the return type of It would seem to me the "correct" solution would be for rtk to either re-export the If it was my library, I'd probably opt for the latter solution. (And this is, in effect, what the solutions here are doing that suggest typing But, in any event, do @markerikson or @phryneas have a view on whether they'd be willing to implement either of these solutions? Or an explanation of why i'm off the mark on that this is a problem (however small)? In fairness the import immer solution as a dev dependency isn't terrible, and as heavy monorepo users we are used to seeing this kind of implicit type dependency when we use third party libs, but rtk is so well typed in general, it always bugs me that it has this little bit of type pollution. 😃 |
we don't export the WriteableDraft type because we export the Draft type. It also wouldn't be correct for the case reducers to change Draft to T - the point is that Draft makes a readonly object mutable, and by changing that you would be able to unsafely pass a readonly state to a mutating case reducer. |
Thanks! Finally found the alternative to WritableDraft after migrating to RTK 2 |
None of the solutions mentioned above worked for me. I ended up doing this as work around. Yes there is an import {
AsyncThunk,
createAsyncThunk,
} from '@reduxjs/toolkit'
export const fetchPosts: AsyncThunk<Array<Post>, void, any> = createAsyncThunk<Array<Post>>(
'posts/fetchPosts',
async () => {
const response = await client.get('/api/posts')
return response.data
}
) Oh and by the way, I ran into this issue while working through the Redux essentials tutorial however I was doing the tutorial in TS instead of JS. |
@Dan503 on a related note, I have an almost-complete rewrite of the "Essentials" tutorial that uses TS here: I'd appreciate any feedback you have on that (over in the PR), especially given that you've just gone through the tutorial. (Also, fwiw, I didn't run into that error myself while updating the example app.) |
None of the above worked for me either. Here's an alternative:
{
"compilerOptions": {
"baseUrl": ".",
"paths": { "@reduxjs/toolkit": ["node_modules/@reduxjs/toolkit/"] },
// ...
}
} |
Can you run this command and try again to see if it fixes the issue: npm install https://pkg.csb.dev/reduxjs/redux-toolkit/commit/06a30ee4/@reduxjs/toolkit |
I had the same issue but when I run this command |
I had this issue and to solve it I just had to add a type to the reducer I want to export. In the presented case, it should be: At least to me that was enough. Hope it helps y'all! |
Please don't do that. You have destroyed all of the types information from the You are no longer getting any benefits from TypeScript by typing |
@TomasDannunzioFIT : no, you absolutely must not do that!!!!. As said, that will remove all TS types and destroy any usefulness. |
This error shows up in VS Code when using TypeScript.
The slice code:
The store config
Versions:
Full error message:
Reinstalling node_modules or clearing the cache didn't help. Do you need any more information?
The text was updated successfully, but these errors were encountered: