-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(nuxt): return types of useAsyncData when using with getCachedData
#25946
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
Conversation
|
|
|
Looks like it breaks the types when using with |
It should be fixed now. |
| * Default is `key => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key]` which only caches data when payloadExtraction is enabled. | ||
| */ | ||
| getCachedData?: (key: string) => DataT | ||
| getCachedData?: <T = DataT>(key: string) => T |
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.
Does this still type hint/constrain the result of this function? If not we might as well return 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.
Nice catch, there's no type constrain any more, I've applied you suggestion to return any, thanks!
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.
Ideally we'd like it to constrain the return, I think.
I guess the issue is that the type of nuxtApp.payload[key] is unknown and ideally when using the function we should either assert that this type matches or perform some sanitisation to ensure it matches. (We might exempt unknown given that this is likely a common situation.)
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.
Agreed, I think constrain and type hint really matters.
I just found the type of data will be influenced by the return of getCachedData, for example
Here the type of data is Ref<boolean | null>
const { data } = await useAsyncData("test2", async () => ({ hello: "world!" }), {
getCachedData: (key) => {
return true
},
});The cache useNuxtApp().payload.data[key] is any, which caused an any data. As of that, I think users should handle the return type of getCachedData if it's 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.
Wouldn't this be a great usecase for the new NoInfer type utility? This would make getCachedData not influence the type of data, but still give hints/constrain the type of getCachedData.
e.g.
type AsyncDataOptions<...> = {
getCachedData?: (key: string) => NoInfer<DataT>;
}As it's a rather new feature (TS 5.4), we might reimplement it like so for now:
// see https://github.com/millsp/ts-toolbelt/blob/master/sources/Function/NoInfer.ts
type NoInfer<T> = [T][T extends any ? 0 : never];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.
what a great idea!
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.
Thanks @94726, I've applied your suggestion, it looks good!
🔗 Linked issue
❓ Type of change
📚 Description
Currently, the data type returned by
useAsyncDatais not inferred correctly when using it with thegetCachedDataoption. This PR fixes the issue.📝 Checklist