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

feat: add types when creating global directives #9660

Merged
merged 4 commits into from
Dec 11, 2023

Conversation

mitjans
Copy link
Contributor

@mitjans mitjans commented Nov 22, 2023

Right now, when you want to create a typed global directive you have to:

import { createApp, type Directive } from 'vue'

const app = createApp(App);

app.directive('custom', <Directive<HTMLElement, string>>{
  mounted(element, { value }) {
    // `element` has type `HtmlElement`
    // `value` has type `string`
  }
});

Also, if you want to create a FunctionDirective, you will have to provide the typings outside, if not, TS will complain:

const custom: Directive<HTMLElement, string> = (element, { value }) => {
  // `element` has type `HtmlElement`
  // `value` has type `string`
};

app.directive('custom', custom);

With this PR, you will be able to provide Directive typings without having to import type Directive and without having to cast the object/function inline:

// Global directive
app.directive<HTMLElement, string>('custom', {
  mounted(element, { value }) {
    // `element` has type `HtmlElement`
    // `value` has type `string`
  }
});

// Function directive
app.directive<HTMLElement, string>('custom', (element, { value }) => {
  // `element` has type `HtmlElement`
  // `value` has type `string`
});

@pikax pikax assigned pikax and unassigned pikax Nov 30, 2023
@pikax pikax added scope: types 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. labels Nov 30, 2023
Copy link

github-actions bot commented Nov 30, 2023

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 86.7 kB 33 kB 29.8 kB
vue.global.prod.js 133 kB 49.8 kB 44.6 kB

Usages

Name Size Gzip Brotli
createApp 48.2 kB 19 kB 17.4 kB
createSSRApp 51.5 kB 20.3 kB 18.5 kB
defineCustomElement 50.6 kB 19.7 kB 18 kB
overall 61.6 kB 23.8 kB 21.7 kB

@pikax pikax added the ready to merge The PR is ready to be merged. label Nov 30, 2023
@yyx990803 yyx990803 changed the base branch from main to minor December 11, 2023 14:06
@yyx990803 yyx990803 merged commit a41409e into vuejs:minor Dec 11, 2023
11 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. ready to merge The PR is ready to be merged. scope: types
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants