feat(types): defineComponent() with generics support#7963
Merged
Conversation
BREAKING CHANGE: The type of `defineComponent()` when passing in a function has changed. This overload signature was rarely used in the past, so it is changed to something more useful. Previously the return type was `DefineComponent`, now it is a function type that inherits the generics of the function passed in. The function passed in as the first argument now also requires a return type of a render function, as the signature is no longer meant to be used for other use cases.
931bef8 to
6ca4b80
Compare
aa900031
reviewed
Mar 28, 2023
5 tasks
IAmSSH
pushed a commit
to IAmSSH/core
that referenced
this pull request
May 14, 2023
BREAKING CHANGE: The type of `defineComponent()` when passing in a function has changed. This overload signature is rarely used in practice and the breakage will be minimal, so repurposing it to something more useful should be worth it. close vuejs#3102
|
is there a example about custom event generic support |
Member
|
Currently we cannot use generic + typed emits or slots (in other words only |
I'm wondering why this issue was not mentioned in the documentation. Or rather, should this situation be included in the document to prevent others from having to spend time like I did to confirm the problem and search for issues? |
|
If the second parameter is given, the generic type does not work. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Breaking Changes
The type of
defineComponent()when passing in a function has changed. This overload signature is rarely used in practice and the breakage will be minimal, so repurposing it to something more useful should be worth it.Previously the return type was
DefineComponent, now it is a function type that inherits the generics of the function passed in. The function passed in as the first argument now also requires a return type of a render function, as the signature is no longer meant to be used for other use cases.Another side effect is that a very specific case of mixins usage breaks:
Note the breakage only happens when calling
defineComponent()inline in themixinsoption on a separately declared plain object. This should be extremely unlikely in userland code because if one wants typed mixins, one will certainly define it like this to get type inference for the mixin itself:This continues to work, so I believe this issue is negligible in practice. It would be nice to fix, but I couldn't figure it out.
Generics Example
Extra Options
This signature is expected to only be used with Composition API. Misc options can be passed via the second argument:
Only
name,inheritAttrs,propsandemitsare allowed.Runtime Props
The final component still need runtime props declared. This can be done automatically via a babel plugin, or manually like so:
The type definition ensures that the manual type and the runtime options must match.
Emits Validation
Type inference based on runtime
emitsoption is supported, including the matchingonXXXprop on the resulting props interface.