You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
declareconstdiscriminated:
|{status: 'resolved';result: string}|{status: 'rejected';error: string}const{ data }=useAsyncData('discriminated',async()=>discriminated)if(data.value.status==='resolved'){// Property 'result' does not exist on type 'Pick<{ status: "resolved"; result: string; }, "status">'.ts(2339)data.value.result}else{// Property 'error' does not exist on type 'Pick<{ status: "rejected"; error: string; }, "status">'.ts(2339)data.value.error}
Describe the bug
Hello! I'm trying to use discriminated unions with useAsyncData and getting an incorrect type of the data ref return from useAsyncData as stated in the reproduction. The resulting type of data is:
The keyof operator in Typescript returns only common keys for discriminated unions. I suggest to give a default type of undefined or null to the PickKeys type parameter of the useAsyncData function. That way we could check if picking is needed in the result type like this:
@roamiiing Thank you for this! Please, do feel free to submit a PR. I'd suggest adding a few extra type tests in test/fixtures/basic/types.ts to ensure we don't break any current types.
Well actually there're more issues related to this. You also can't pick keys that were introduced in transform function because of some weird behaviour of TS type params.
I'm currently investigating how to fix all of that with minimal changes.
Environment
Darwin
v16.14.0
3.0.0-rc.8
[email protected]
vite
srcDir
,modules
,vite
,build
@pinia/[email protected]
,@nuxtjs/[email protected]
,unplugin-icons/nuxt
,@nuxtjs/[email protected]
,@nuxtjs/[email protected]
-
Reproduction
Describe the bug
Hello! I'm trying to use discriminated unions with
useAsyncData
and getting an incorrect type of thedata
ref return fromuseAsyncData
as stated in the reproduction. The resulting type ofdata
is:The only field that is picked from a union is
status
which exists in both union cases. I think that issue starts here: https://github.com/nuxt/framework/blob/1700bf822e31ffe2c9ec66b61d6f6562d45dcf2b/packages/nuxt/src/app/composables/asyncData.ts#L15The
keyof
operator in Typescript returns only common keys for discriminated unions. I suggest to give a default type ofundefined
ornull
to thePickKeys
type parameter of theuseAsyncData
function. That way we could check if picking is needed in the result type like this:And the typings for discriminated unions become correct:
I could submit a PR if this approach is acceptable.
Additional context
No response
Logs
No response
The text was updated successfully, but these errors were encountered: