-
-
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
Action payload is still possibly undefined even when a value is given #399
Comments
Paging @phryneas ... |
I'm going to take a guess and say that you have setCounterAndStep: (state, action: PayloadAction<TestType | null | undefined> | null | undefined) => { So our types are handling your code as that. Let's step back and take a look at what should happen in that case: In the case that a user types their PayloadAction as
The overload that is puzzling you is there to allow for 2. in these cases. I've written that half a year ago, so I'm not 100% sure any more, but I guess it couldn't be defined as just the second overload, as the Anyways: I would really suggest you turn on |
That was it! Enabling |
If you find a way of handling that that works well with both |
Hey @maxijonson, I put in a PR that might improve the autocompletion for people with
|
Just tested it and it works! I get autocompletion with the correct types and actions that do not have a payload will fail if I try to pass one in (as expected). Thanks you very much! 🙂 |
Considering this example:
When calling the
setCounterAndStep
action with an empty object as payload... (or an incomplete object)the function tries to apply the
payload
type to undefined AND TestType, which comes from two function overloads when it really should be applying it only to TestType, since an empty object is not undefined.Additionally, Intellisense fails to suggest
counter
andstep
because of theundefined
overload.When trying to understand why this happens, I stumbled upon the
ActionCreatorWithOptionalPayload
interface of typings.d.ts. It seems the first overload is described byand the second by
I found the first overload strange... Why would the
payload
be optional AND typed asundefined
? I think there should only be the second overload, which already haspayload
optional, so it has the same effect as the first overload.I removed the first overload from the typings and it fixed the issue. All of these calls work as expected:
Additionally, Intellisense now suggests correctly
counter
andstep
forsetCounterAndStep
.Is there something I am not seeing? Why do we need the first overload in the first place when we can just leave
payload
optional on the second overload and have the same effect?Using TS 3.7.5, also tried with TS 3.8.2
The text was updated successfully, but these errors were encountered: