-
-
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
Use void
instead of any
for undefined payloads
#174
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit e9a9ad3 https://deploy-preview-174--redux-starter-kit-docs.netlify.com |
Hmm. A bunch of type tests fail because of this change. I'm not clear what improvement this is supposed to make. Going to close this for now. If you still feel it's a point of concern, please let me know and we can discuss further. |
@markerikson thanks for taking a look, we've made this exact patch in our project using this library, and it revealed a bunch of missed or mismatched action payload typings, while forcing us to specify payload where it was expected. I would look into fixing the tests if we can agree this would be beneficial to have merged. |
Sure, sounds good. Hmm. I don't suppose this is related to #211 at all? |
@phryneas : since you're consulting, any thoughts on this one? |
So looking at the type errors, some of them from "raw" uses of const action: PayloadAction = { type: '', payload: 5 }
const numberPayload: number = action.payload Also: const increment = createAction('increment')
// Error: Argument of type '1' is not assignable to parameter of type 'void | undefined'
const n: number = increment(1).payload Those are fixable by adding generic to |
Well, it would definitely change the defined behaviour. Especially this test defines that a PayloadAction should fall back to I guess all other of those tests are building on this "acceptance criteria" that was formulated in this first test. As for me, I'd like this to change, as those types being more strict will prevent bugs like people accidentally forgetting the type parameter and then doing werid stuff. Your call if you want this :) |
This does not seem related, as the PR would only cover the |
I guess it would help if I better understood the ramifications of the type change, in terms of end-user experience, actual code usage, and tests that would have to change. Can anyone add some clarification? |
I think basarat describes this a whole lot better than me: https://basarat.gitbooks.io/typescript/docs/options/noImplicitAny.html The point is: he describes The question is do we allow all our users to do something potentially dangerous by accident, or do we require them to explicitly do that dangerous thing if they really want to?
As for the tests: currently, it seems to be a "business requirement" that it works as it currently does. I don't know who initially formulated it like this, and what they had in minde 🤷. I guess a few test would need to be replaced with other tests. But I also guess that would be fine. |
Yeah, I think I can get behind making the typing more explicit here, especially since most folks would be using Would one of you be able to tackle this? I'd want to get it into 0.8 as well, but I want to start working on updating the tutorial contents. |
Okay, I re-created the branch off of |
@@ -22,16 +22,19 @@ function expectType<T>(p: T): T { | |||
* Test: PayloadAction type parameter is optional (defaults to `any`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaults to any
-> should not default to any
@@ -58,10 +61,12 @@ function expectType<T>(p: T): T { | |||
payload | |||
}), | |||
{ type: 'action' } | |||
) as PayloadActionCreator | |||
) as PayloadActionCreator<number> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be number|undefined
|
||
expectType<PayloadAction<number>>(actionCreator(1)) | ||
// typings:expect-error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and these two should actually not fail then
@@ -110,22 +115,21 @@ function expectType<T>(p: T): T { | |||
} | |||
|
|||
/* | |||
* Test: createAction() type parameter is optional (defaults to `any`). | |||
* Test: createAction() type parameter is required, not inferred (defaults to `void`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test doesn't really make any more sense (description does not match what it tests right now), so I would delete it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, lemme rewrite this one
Looks good from my side. |
great, thanks! |
* Create slice changes (#197) * Include case reducers in createSlice result (#209) * Fix missing name field in type test * Include provided case reducers in createSlice output - Added `caseReducers` field to the createSlice return object - Added test and type test for returned case reducer - Removed "Enhanced" terminology from types, and replaced with "CaseReducerWithPrepare" and "CaseReducerDefinition" - Restructured logic to only loop over reducer names once, and check case reducer definition type once per entry * Add type tests for correct case reducer export inference * Rewrite caseReducers types for correct inference of state + action * Add type test for reducers with prepare callback * Fix type inference for actions from reducers w/ prepare callbacks * Clean up type names and usages * Use a generic State type in ActionForReducer * Re-switch Slice generics order * Use `void` instead of `any` for undefined payloads (#174) * Change default createAction payload type to void to make it required * Fix createAction type tests per review * Update tutorials for v0.8 (#213) * Update basic tutorial with createSlice changes * Update intermediate tutorial with createSlice changes * Update advanced tutorial with createSlice changes * Change existing commit links to point to reduxjs org repos
Additional layer of type security, don't let the user implicitly opt out of type checking