Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@fenghan34
Copy link
Contributor

@fenghan34 fenghan34 commented Feb 25, 2024

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Currently, the data type returned by useAsyncData is not inferred correctly when using it with the getCachedData option. This PR fixes the issue.

📝 Checklist

  • I have linked an issue or discussion.
  • I have added tests (if possible).
  • I have updated the documentation accordingly.

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@fenghan34 fenghan34 marked this pull request as draft February 25, 2024 15:16
@fenghan34 fenghan34 marked this pull request as ready for review February 25, 2024 15:34
@fenghan34
Copy link
Contributor Author

Looks like it breaks the types when using with transform option, I'm looking into it.

@fenghan34
Copy link
Contributor Author

Looks like it breaks the types when using with transform option, I'm looking into it.

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
Copy link
Member

@danielroe danielroe Feb 26, 2024

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.

Copy link
Contributor Author

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!

Copy link
Member

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.)

Copy link
Contributor Author

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.

Copy link
Contributor

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];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what a great idea!

Copy link
Contributor Author

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!

@danielroe danielroe marked this pull request as draft March 15, 2024 15:29
@danielroe danielroe marked this pull request as ready for review April 8, 2024 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useAsyncData types are inferred from getCachedData and not from the handler

4 participants