-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
types(defineComponent): Stricter Component Type + helpers #9556
base: minor
Are you sure you want to change the base?
Conversation
Size ReportBundles
Usages
|
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
# Conflicts: # packages/runtime-dom/src/apiCustomElement.ts
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Will this help resolve question I asked? unovue/shadcn-vue#277 Thanks for TS Magic 🪄 |
@jd-solanki no, the issue you linked is related with the vue compiler being able to infer props like that, bear in mind the compiler must know all the available props ahead of time ,to be able to generate the vue props object. When you extend or implement the type might lose that information. |
@pikax Hey! Please check the Svelte type helpers https://github.com/wobsoriano/svelte-sonner/blob/main/src/lib/types.ts#L30C2-L31C51 What is the equivalent of this source with your Vue type helpers? Thanks and sorry for tag 🙏 |
@sadeghbarati what type are you looking for, the two lines you sent one is the component and the other is props, we have:
|
I have something like this in Vue to get props, I have to use export type ToastT<T extends Component = Component> = {
component?: T; // T is Vue component but how to get types without typeof
componentProps?: ComponentProps<typeof T>; // 'T' only refers to a type, but is being used as a value here.
} Please take a look at svelte-sonner |
Hey! Any news on this? It would be cool to use and test generics! Without this merge, vuejs/test-utils#2254 cannot be closed, and we cannot really use generics. Can I help in some way to move this forward? |
closes #7259 #9296
Breaking Changes
This PR causes breaking changes, because the types become more stricter on
Component
in general, this is necessary to prevent false type safety by preventing theDefineComponent
andComponent
from beingany
.Ecosystem
There's companion PRs for the ecosystem
Preliminary work as been done, once 3.5 alpha is released I'll update the PRs to add the alpha as dependency.
I expect Component libraries to be broken, please reach out to me through Discord on
#typescript
channel, tagging @pikax.Details
Added the ability to extract the original options which the component was created:
This will be used to build the component types helpers
Improvements
defineComponent
defineComponent
has become more stricter, now is possible to extract component options as they were passeddefineAsyncComponent
defineAsyncComponent
got it's types improved now correctly exposes it's own type as options and on rendering the innerComponent.defineCustomElement
defineCustomElement
improvement on resolving theprops
for passed components, it should correctly infer props from passed components.Helpers
There's 2 types of helpers introduced,
Extract*
andComponent*
, theExtract
will extract the original type passed to define the component, theComponent
will provide the typescript type.Extract
can be used to enhanced the component options, eg: add more propsExtractComponentOptions
Extract the original options from the component.
ExtractComponentProp
Extracts the object passed to create options or if options are not available it will provide the
typescript
object definition.ExtractComponentSlots
Extracts original slot options from the component, if no options are available it won't be able to infer them, since slots are not exposed on functional components, that information is lost on
defineComponent
ExtractComponentEmits
Extracts original emits options from the component, if no options are available it won't be able to infer them, since emits are not exposed on functional components, that information is lost on
defineComponent
. You can useComponentProps
to get the runtime type, props on functional components includeson*
.ComponentProps
Provides the typescript representation of component props, it appends emits props by default.
ComponentSlots
ComponentEmits
DeclareComponent
DeclareComponent
is an helper to create Vue components through types without needing to pass 16 arguments to the type.It contains 5 arguments:
Props
It allows to pass just props object or a new Component generic, if a component is passed it will short-circuit the type.
Data
Data exposed publicly when using template
:ref
.Options
Allowing to Provide custom properties to the Options object.