feat(types): support nuxt types for defineComponent
#19789
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π Linked issue
β Type of change
π Description
Support
asyncData
types in components exported usingdefineComponent
function.The existing code only handled
Vue.extend
which is no longer recommended in Vue 2.7 and also Volar defaults todefineComponent
so theasyncData
types didn't work properly in that case by default (unless manually wrapped withVue.extend
).This adds new overloads for
defineFunction
that are slightly modified to supportasyncData
option, including inferring types both in the script tag and in the template. The original Vue 2.7 types have 5 overloads ofdefineComponent
. I've added variants of first 3, skipping functional component cases. Since functional components don't have state, I thinkasyncData
doesn't apply to those but maybe I'm wrong? Haven't investigated that.Also added tests for both the
definedComponent
andVue.extend
cases.This is equivalent to testing types within the "script" tag in the Vue SFC component. I haven't quite figured out how to test the "template" part but this is better than nothing.
To better visualize what modifications were done to the original overloads I've created this diff that shows a diff between the original and the "augmented with asyncData" types:
defineComponent Diff
Oh, and I've also changed
asyncData
to not haveVue
instance asthis
as that's not accurate.this
isundefined
inasyncData
.π Checklist