-
-
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
Q: createAction with payload typings #211
Comments
Hmm. Yeah, I would generally expect that last one to be an error. I know there's some trickiness around how the action creators get defined, but I'm not clear on the details myself. @phryneas , @Jessidhia, @denisw : any idea what's happening here? |
I just tested this and at least on master, this seems to work. Are you maybe not on the most current version of RSK? |
Thanks for looking at this. @phryneas I created a repro here: https://codesandbox.io/s/nervous-frost-rczon?fontsize=14 Ignore the react setup, just used it as a base, check out the |
Weirdly, this doesn't trigger a test i added right now, but in your repro i can see it. Seems like a problem with the IfMaybeUndefined type. Looking into it. |
okay, the following type only works when the compilerOption type IfMaybeUndefined<P, True, False> = [undefined] extends [P] ? True : False; I'm not sure if that CAN be tested without the strictNullChecks flag though. Firstly, could you please validate if setting the flag helps you with your problem? |
That works @phryneas! Sorry for the false positive. 🙇 |
As this is out of my knowledge to resolve, I've posted a SO question - maybe someone else knows if this is testable, even with strictNullChecks: false. We'll see. If it is possible, I'll do a PR later. |
is there any update on this? is it possible without using strictNullChecks? |
@wuifdesign : what version of RSK are you using? We merged in some changes in #174 that might fix this. Make sure you're using v1.0.1 - does that fix the issue? |
It's not possible without strictNullChecks. The answerer of my above SO question quoted the TS documentation
So, even if you're writing createAction<number>('INCREMENT_BY'); without createAction<number|null|undefined>('INCREMENT_BY'); so from our side, there is no way to distinguish if you WANTED to write So, we're always handling it as if you had written There's really no way around this - but if you're not using strictNullChecks, honestly this should be the least of your problems. I really suggest you evaluate turning it on. |
Or, technically, we could go from |
v1.0.1 dosn't fix this issue. it is just a possible cause of erros if this check isn't working, for example you can do this without TS error:
this will end up in a "NaN" in the store because action.payload will be undefined. but with TS we should be able to catch this error before having to debugg it in runtime. if i do:
maybe the types have to be rewiritten somehow to do a different check for the "undefined", but i don't know how. |
Yet, if you do function myManualActionCreator(): { payload: number } {
return { payload: undefined };
} TS with This is just your decision to disable a fundamental type security feature of TypeScript. From that point on you'll have to double-check everything you do as a dev. The typings are stating here: if the payload of the resulting action is optional, the method may be called without an argument. |
I'm trying to type an action with something like:
Why is the last one not an error? How can I type it in a way that payload is required?
The text was updated successfully, but these errors were encountered: