-
-
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
Improve typings: Added ability to typecast action types in createSlice
#136
Conversation
no more ts-ignore
actionCreators implementation and type signature
added String literal type param to createSlice type signature for typing slice. changed `CaseReducerActionPayloads` type to `CaseReducerActions` to extract entire action.
No foreseeable benefits and thus just technical debt
Deploy preview for redux-starter-kit-docs ready! Built with commit 3562ff2 https://deploy-preview-136--redux-starter-kit-docs.netlify.com |
so payload types `undefined`, `never` and `void` have action creator type signatures requiring no payload
Pinging @denisw, @Jessidhia, @hedgerh, @markerikson |
Will take a look when I get home later 👀 |
For the still-mostly-TS-clueless: can I get a comparison of both the general usage and the type-checking status before and after this PR? What do things look like now, and what do they look like as a result of this? |
Handling `never` is not worth the complication
@markerikson The changes introduced are not breaking for TS users, in fact unless told about it most people won't notice it. Previously you would typecast your reducers actions like this const formSlice = createSlice({
slice: 'form',
reducers: {
setName: (state, action: PayloadAction<string>) => {
state.name = action.payload;
},
},
}); formSlice.actions.setName would have the following type signature: With this PR, first of all, the Second, users can now optionally typecast the action const formSlice = createSlice({
slice: 'form',
reducers: { // Talking about \|/
setName: (state, action: PayloadAction<string, 'form/setName'>) => {
state.name = action.payload;
},
},
}); So Notice that the |
What happens if you intentionally misspell the action type in the case reducer args annotation? Sorry didn’t have a chance to look at this last night. About to take a look |
What does current usage look like? when creating a slice, do you just assert
So we don't lose the ability to assert the shape of just the payload if we aren't concerned with the action type, correct? @markerikson this is useful for someone who needs to make use of these actions elsewhere and doesn't want to create a whole new interface for it. I would use this like so:
@Dudeonyx we may need some |
I need some advice from you TS experts on this one. You think it's worth merging / ready to merge? |
Hopefully @Dudeonyx can get back about my last comment. I'll pull this down tonight and see how it's looking, though. |
I'd still appreciate some further feedback on this PR. |
Following up again. Is this still relevant after all the other recent changes? |
This is still relevant, as the recent changes did not affect the It still bears the problem of user-specified types potentially not matching the real, generated type. But as TS has no type-string-concatenation, this is the best way to currently do this and TS cannot check those. And at least, an error would be located better directly in the slice, close to the PS: as already mentioned, doing it this way works only for reducers that actually specify their action. This could be solved by adding a type |
Well, this has been sitting around for months. I'll continue to leave it open for now, but unless someone can take the time to fix up the conflicts, I don't see it getting into 0.8 or 1.0. |
Actually, looking at the current source, is this already covered? export type PayloadAction<
P = any,
T extends string = string,
M = void
> = WithOptionalMeta<M, WithPayload<P, Action<T>>> Meh. I'm going to go ahead and close this. If someone can take the time to review the current behavior in comparison, I'm still open to trying to improve it if possible. |
* Use code-generation everywhere
Referencing issue #134 about improving typings while not changing the core api just for typescript users.
This PR adds the ability to typecast action types in createSlice, works as follows
giving the following action creator type signatures in formSlice.actions
Note: payload types

undefined
,never, andvoid
have action creator signature types requiring no payloadsCaveat: Possibility of user error in typecasting exists but until typescript supports string literal concatenation not much can be done.
Bonus: I 'fixed' the typescript issue in
createReducer
, so no more need to// @ts-ignore
that part, Fix has no impact on exposed api's or types.Bonus 2: the
slice
argument and return increateSlice
is now a string literal, hooray for slightly barely better type safety