-
-
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
Typescript: createAsyncThunk type infer not working correctly in 1.6.0 with multiple enum #1156
Comments
Yes, it's possible that the behaviour changed here slightly. It should work if you add a return value like: - export const bug = createAsyncThunk("foo/bar", async () => {
+ export const bug = createAsyncThunk("foo/bar", async (): Example => {
if (Math.random() > 0.5) {
return Example.A;
} else {
return Example.B;
}
}); Does that sound like an acceptable solution for you? |
Its acceptable to me, but i just pointed breaking change. |
1.6.1 broke one of my apps too. Type inference was not working anymore with an asyncthunk function returning a boolean |
Currently even annotating the async function doesn't work in my case. I'm stuck with 1.5.1 at the moment |
@gtestault seems to be a different problem then. Can you share code or even a reproduction? |
throws this:
|
I have no idea what TypeScript is doing there, going from Meanwhile, export const fetchIsAdmin = createAsyncThunk<boolean, void>(
'session/isAdmin',
async () => {
const response = await checkAdminRights()
return response.data
}
) should do the trick. |
Passing generic types into the function or having to define the return type is, to me, the crux of this issue and I consider 1.6 a breaking change for sure. Many of our AsyncThunks are now errors, where in 1.5 the correct return type was inferred. Additionally, we use a factory to bind the state and some other types into export const fetchFoo = createAsyncThunk(
'foo',
async ({ id, optionalProp = 'defaultProp' }: { id: string, optionalProp?: string }, { dispatch, extra: { http } }) => {
const data = await dispatch(http.get<SomeReturn>('/foo', {id, optionalProp}))
return data
} In this example, the return type is hinted to I'm seeing a bunch of issues with return types in 1.6 when nothing in our code has changed. For example, union types are failing and it's showing the same sorts of errors as OP. Anything like: enum AorBProp {
A = 'A',
B = 'B',
}
type A = { prop: AorBProp.A }
type B = { prop: AorBProp.B }
type AorB = A | B Results in the error:
In this case, even giving the return type explicitly does nothing to fix the situation. I'm happy with some of the improvements in 1.6, but this sort of breaking change should not have made it into a feature release. |
Here's a follow up repro: enum Foo {
A = 'A',
B = 'B',
}
type A = { prop: Foo.A }
type B = { prop: Foo.B }
type AorB = A | B
// Error
const a = createAsyncThunk('foo', () => Promise.resolve<AorB>({ prop: Foo.A })) Also note that createAsyncThunk('foo', (_: void, { rejectWithValue }: { rejectWithValue: RejectWithValueFn<AorB> }) => ({})) It has to be defined through generics. That means we have to explicitly define |
@bfricka Dealing with types is, frankly, complex. It gets even more so when you as an end user are trying to wrap and reuse types from a library. We do our best to avoid anything that might accidentally break user code, but honestly, sometimes these things happen. We can't anticipate every way that users will use our APIs, or every possible interaction. That's the nature of software development. We have a ton of tests for both runtime and types behavior, and we put out several alphas and betas asking for feedback. No bugs were reported around this behavior. What would help at the moment is reproductions that demonstrate code that worked in 1.5 and breaks in 1.6, so that we can look at those and try to figure out options for improving behavior (and add tests to ensure this doesn't break again in the future). You've given a couple of those, and if you could provide any more as CodeSandboxes or repos (especially with some of these more complex use cases) that would be helpful. |
You're entirely correct. I apologize for coming off as ranty, and I was
actually thinking this as I was writing earlier, but I didn't convey it for
whatever reason.
The types for this project are very complex, but that let's them do some
wonderfully powerful things. I'm definitely aware of that, and I'm super
clear that there's no way you can model out all the ways in which people
could use the types.
I will do my best to try to tease out the root of these issues. I've found
what I think is the problem area, but it might take me some time to find a
solution that will preserve the desired type behavior of all of the
existing use cases. Along the way, if I run into any issues, I'll post
codesandbox reproductions and describe where I'm at.
Thank you
…On Fri, Aug 27, 2021, 16:46 Mark Erikson ***@***.***> wrote:
@bfricka <https://github.com/bfricka> Dealing with types is, frankly,
complex. It gets even more so when you as an end user are trying to wrap
and reuse types from a library.
We do our best to avoid anything that might accidentally break user code,
but honestly, sometimes these things happen. We can't anticipate every way
that users will use our APIs, or every possible interaction. That's the
nature of software development.
We have a ton of tests for both runtime and types behavior, and we put out
several alphas and betas asking for feedback. No bugs were reported around
this behavior.
What would help at the moment is reproductions that demonstrate code that
worked in 1.5 and breaks in 1.6, so that we can look at those and try to
figure out options for improving behavior (and add tests to ensure this
doesn't break again in the future). You've given a couple of those, and if
you could provide any more as CodeSandboxes or repos (especially with some
of these more complex use cases) that would be helpful.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1156 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFIJHYYUVBSZ4E6ARDAP2LT7APVPANCNFSM46MDCPYQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
TypeScript itself has breaking changes to all users of TypeScript in every minor version, similarly we also sometimes cannot avoid them on type level. That said, I have looked into this and believe I have found a fix in #1449. Please try out the CodeSandbox CI build and report back. |
Unfortunately, while this may fix the boolean issue it doesn't appear to fix the enum + type union issue that I described above. I applied the Edit: I take it back. This was an IDE issue. While there are still some type issues I'll need to work through with the new |
As I said - best install the CodeSandbox build - the link above has install instructions, it's really just |
Sorry about that. I'm just unfamiliar with that workflow and took a shortcut, which obviously didn't pay off. Thank you for finding a fix for this so quickly. |
In 1.5.1 work fine, in 1.6.0 not.
If explicitly typed, everything ok.
The text was updated successfully, but these errors were encountered: