-
-
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
Type errors with createSlice in TypeScript 3.4.1 #131
Comments
I'm also having issues with const slice = createSlice({
initialState: {
data: []
},
reducers: {
setData: (state, action: PayloadAction<any[]>) => {
state.data = action.payload
}
}
}) The above produces an error: |
@denisw , @Jessidhia , @Dudeonyx : any thoughts? |
@Rinaldo yours is a separate issue. The type of const slice = createSlice({
initialState: {
data: [] as any[]
},
reducers: {
setData: (state, action: PayloadAction<any[]>) => {
state.data = action.payload
}
}
})
//
// or
//
interface State {
data: any[];
}
const initialState: State = {
data: [],
}
const state = createSlice({
initialState: initialState,
reducers: {
setData: (state, action: PayloadAction<any[]>) => {
state.data = action.payload
}
}
}) |
@pat-son I had tried the first option before posting and just now tried the second one. Neither of those snippets work for me, they both still produce the same error. running redux-starter-kit 0.4.3 and typescript 3.4.2 |
I'd be happy to help but I'm out of town without a system until tomorrow
evening...
@Rinaldo have you tried `createSlice` with type parameters?
I don't recall the exact signature but it should be something like
```js
const { reducer } = createSlice<Actions,State, Something>({
//...
});
```
|
@Dudeonyx That didn't seem to help, and I also noticed the same error with createReducer. const initialState = {
data: [],
}
const reducer = createReducer(initialState, {
setData: (state, action: PayloadAction<any[]>) => {
state.data = action.payload // TS2322: Type 'any[]' is not assignable to type 'never[]'
}
}) It doesn't seem to matter whether the state is explicitly typed or not and it seems to only be a problem with arrays. Other types come through fine. Should I open another issue or keep the discussion here? |
Interestingly, the example works compiles fine in TypeScript 3.2.2 (the one we test against within redux-starter-kit), and the overall action type of the slice is inferred as I suspect the change in behavior stems from a change in 3.4:
Sounds related, but I'm not 100% sure. Anyway, a pragmatic solution would be to change the type signature of |
I think I found a way to restructure the typings so that the |
I've been fully busy with React-Redux stuff (and real work!) lately, but I will try to swing through and deal with several outstanding RSK PRs / issues within the next few days. |
* Improve type inference of case reducers Previously, the TypeScript compiler would reject case reducer maps with different incompatible PayloadAction types. The case reducers map and createReducer() / createSlice() types have now been restructured to allow for better type inference. Fixes #131 * Upgrade TypeScript and ESLint parser Upgraded TypeScript to 3.4.3 and switches to the new TypeScript ESLint parser.
When the reducers in a
createSlice
have mixed payload action types, I get weird type errors as of TypeScript 3.4.1 (Maybe 3.4.0 as well, I didn't check).It's like it's assuming that all
PayloadAction
s it sees should be the same type as the first one.Code for reproduction:
The text was updated successfully, but these errors were encountered: