Skip to content
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): Support for GlobalComponents, typed Directives and respect expose on defineComponent #3399

Merged
merged 43 commits into from
Apr 25, 2024

Conversation

pikax
Copy link
Member

@pikax pikax commented Mar 9, 2021

fix #3367

Changes

  • Support for typed Components - to allow extraction of the components available
  • Support for typed Directives and ComponentCustomDirectives (aka global directives)
  • Initial support for typed exposed - this is a bit different from the actual implementation, this types will allow the exposed vue internal component types
  • Improvements on Directive type, allowing to specify the modifiers and the expected argument.

Directive usage

Now Directives will hold more types to help the extensions:

export type Directive<
  HostElement = any,  // host Element type, eg: `HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement`
  Value = any, // value typed, eg: `string` or `number`
  Modifiers extends string = string, // possible modifiers, eg: `'trim' | 'number' | 'lazy'`
  Arg extends string = string, // possible `arg`, eg: `string` or `'passive' | 'other'`
> =
 // implementation 
  | ObjectDirective<HostElement, Value, Arg, Modifiers> 
  | FunctionDirective<HostElement, Value, Arg, Modifiers>
  
  
// vmodel type implementation
Directive<HTMLInputElement | HTMLTextAreaElement, any, 'trim' | 'number' | 'lazy'> 

@pikax pikax marked this pull request as ready for review March 9, 2021 12:45
@johnsoncodehk
Copy link
Member

Thanks for this update, custom directives IntelliSense and Type-Checking is coming!

We also need ComponentCustomComponents (global components). Similar to JSX.IntrinsicElements but actually it's use for handle different problem.

@HcySunYang
Copy link
Member

HcySunYang commented Mar 10, 2021

We also need ComponentCustomComponents (global components). Similar to JSX.IntrinsicElements but actually it's use for handle different problem.

@johnsoncodehk I think this is worth putting in a new issue for tracking, with as detailed information as possible. 😁

Edit: After reviewing the code, I think it can be implemented in this PR if possible

@pikax
Copy link
Member Author

pikax commented Mar 10, 2021

We also need ComponentCustomComponents (global components).

Maybe a better name would be good, any suggestions?

@HcySunYang
Copy link
Member

GlobalComponents and GlobalDirectives?

@johnsoncodehk
Copy link
Member

GlobalComponents and GlobalDirectives?

Look good, but inconsistent...

declare module 'vue' {
  interface ComponentCustomOptions { }
  interface ComponentCustomProperties { }
  interface GlobalComponents { }
  interface GlobalDirectives { }
}

@pikax
Copy link
Member Author

pikax commented Mar 11, 2021

Look good, but inconsistent...

It looks inconsistent but they mean different things

  • ComponentCustomOptions : Component custom options when you defineComponent, eg: onBeforeRouteUpdate
  • ComponentCustomProperties : Custom vm properties available on the componentInstance, eg: $router
  • GlobalComponents : Components added to the app/global, available on every component (render), eg: router-view
  • GlobalDirectives : Directives defined to the app/global, available on every component (render), eg: v-tooltip

As you can see they usually mean different things, altho the Components/Directives are used on the components, they are not enhancing the component interaction, but to provide enhancement to the render.

I mean this is my logic on the naming, might be incorrect 😄

Having ComponentCustomComponents sounds a bit bad 😆

@HcySunYang
Copy link
Member

yeah, they are different.

@pikax pikax requested a review from HcySunYang March 13, 2021 09:35
@HcySunYang HcySunYang added the 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. label Mar 29, 2021
jpntex added a commit to indielayer/ui that referenced this pull request Jun 28, 2023
* Make volar understand the global components

Following volar documentation the declared module shouldn't be "vue" but rather "@vue/runtime-core".
https://marketplace.visualstudio.com/items?itemName=Vue.volar
vuejs/core#3399

* Update packages/ui/volar.d.ts

---------

Co-authored-by: João Teixeira <joao@indielayer.com>
@pikax pikax assigned pikax and unassigned pikax Oct 19, 2023
pikax and others added 3 commits October 21, 2023 10:42
# Conflicts:
#	packages/dts-test/componentTypeExtensions.test-d.tsx
#	packages/dts-test/defineComponent.test-d.tsx
#	packages/dts-test/directives.test-d.ts
#	packages/runtime-core/__tests__/componentPublicInstance.spec.ts
#	packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
#	packages/runtime-core/src/apiDefineComponent.ts
#	packages/runtime-core/src/componentOptions.ts
#	packages/runtime-core/src/componentPublicInstance.ts
#	packages/runtime-core/src/hydration.ts
#	packages/runtime-core/src/index.ts
#	packages/runtime-dom/src/directives/vModel.ts
#	packages/runtime-dom/src/globalComponents.d.ts
#	packages/runtime-dom/src/globalDirectives.d.ts
#	packages/shared/src/typeUtils.ts
@github-actions
Copy link

github-actions bot commented Oct 21, 2023

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 93.9 kB (+3.24 kB) 35.5 kB (+966 B) 32 kB (+877 B)
vue.global.prod.js 151 kB (+3.24 kB) 54.8 kB (+999 B) 48.9 kB (+809 B)

Usages

Name Size Gzip Brotli
createApp 53.9 kB (+3.16 kB) 20.9 kB (+1.01 kB) 18.9 kB (+838 B)
createSSRApp 57.2 kB (+3.16 kB) 22.2 kB (+983 B) 20.1 kB (+802 B)
defineCustomElement 56.2 kB (+3.16 kB) 21.6 kB (+999 B) 19.6 kB (+802 B)
overall 67.7 kB (+3.21 kB) 25.8 kB (+958 B) 23.4 kB (+854 B)

pikax and others added 3 commits October 21, 2023 11:19
# Conflicts:
#	packages/dts-test/defineComponent.test-d.tsx
#	packages/runtime-core/src/apiDefineComponent.ts
#	packages/runtime-core/src/componentPublicInstance.ts
@yyx990803 yyx990803 changed the base branch from main to minor November 21, 2023 00:39
@pikax
Copy link
Member Author

pikax commented Nov 22, 2023

/ecosystem-ci run

@vue-bot

This comment was marked as outdated.

# Conflicts:
#	packages/dts-test/appUse.test-d.ts
#	packages/dts-test/defineComponent.test-d.tsx
#	packages/runtime-core/src/apiCreateApp.ts
#	packages/runtime-core/src/apiDefineComponent.ts
#	packages/runtime-core/src/componentOptions.ts
#	packages/runtime-core/src/componentPublicInstance.ts
#	packages/runtime-core/src/directives.ts
#	packages/runtime-core/src/index.ts
#	packages/runtime-dom/src/directives/vModel.ts
#	packages/runtime-dom/src/directives/vOn.ts
#	packages/runtime-dom/src/index.ts
@matthew-dean
Copy link

Can this be merged?

@matthew-dean
Copy link

matthew-dean commented Jan 29, 2024

Note, one thing I figured out you can do is simply use TypeScript's satisfies to get type-checking for custom directives. For example:

<div 
  v-popover="{
    html: true,
    title: vm.tooltipTitle(),
    placement: 'right',
    trigger: 'click',
    content: vm.tooltipContent()
    } satisfies PopoverOptions"
></div>

Volar's code syntax coloring of Vue files completely borks if you use satisfies in that way, but it looks like the technique itself succeeds.

It would be great if there was an ESLint plugin then to force / enforce the use of satisfies for any custom directive in the meantime (I guess from the eslint-plugin-vue folks) but that's a separate issue.

@pikax pikax changed the title types(defineComponent): support for expose component types types(defineComponent): Support for GlobalComponents, typed Directives and respect expose on defineComponent Apr 17, 2024
@pikax
Copy link
Member Author

pikax commented Apr 17, 2024

/ecosystem-ci run

@vue-bot

This comment was marked as outdated.

@vue-bot

This comment was marked as outdated.

@vue-bot
Copy link

vue-bot commented Apr 25, 2024

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools failure failure
nuxt success failure
pinia success success
primevue success success
quasar success success
radix-vue success success
router success success
test-utils success success
vant failure success
vite-plugin-vue success success
vitepress success success
vue-i18n success success
vue-macros success success
vuetify failure failure
vueuse success success
vue-simple-compiler success success

@yyx990803 yyx990803 merged commit 4cc9ca8 into vuejs:minor Apr 25, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. scope: types version: minor
Projects
Development

Successfully merging this pull request may close these issues.

Expose types of locally registered components