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(types): enhance the event type in defineEmits #8332

Closed
wants to merge 5 commits into from

Conversation

nooooooom
Copy link

@nooooooom nooooooom commented May 16, 2023

closes vuejs/language-tools#3197

Enhance the event type in defineEmits so that it conforms to the triggering mechanism of emit.

For normal events, we allow them to contain camelized copies:

defineEmits(['value-change'])
->
{
  'value-change': () => void
  valueChange: () => void
}

For v-model update:xxx events, we allow them to contain hyphenate copies:

defineEmits(['update:modelValue'])
->
{
  'update:modelValue': () => void
  'update:model-value': () => void
}

It also filters the types that already exist and are defined in events:

defineEmits({
  'value-change': () => void
  valueChange: null
})
->
{
  'value-change': () => void
  valueChange: (value: any) => void
}

@yyx990803
Copy link
Member

Thanks for the PR - could you please add type tests in https://github.com/vuejs/core/blob/main/packages/dts-test/setupHelpers.test-d.ts?

@nooooooom
Copy link
Author

Thanks for the PR - could you please add type tests in https://github.com/vuejs/core/blob/main/packages/dts-test/setupHelpers.test-d.ts?

I'm planning to do this!

@nooooooom
Copy link
Author

This feature is imperfect because I do not know how to change the parameter type for an overloaded function. 🤔

@nooooooom
Copy link
Author

This feature is imperfect because I do not know how to change the parameter type for an overloaded function. 🤔

All usage scenarios are currently supported!

@yyx990803
Copy link
Member

yyx990803 commented May 18, 2023

After testing this locally, it seems this doesn't actually fix vuejs/language-tools#3197 - the actual issue seems to be in Volar, not in the types of defineEmits.

The kebab-case and camelCase conversions are mostly meant for parent consumers. Locally, defineEmits should just keep the exact event names for consistency.

@nooooooom
Copy link
Author

After testing this locally, it seems this doesn't actually fix vuejs/language-tools#3197 - the actual issue seems to be in Volar, not in the types of defineEmits.

The kebab-case and camelCase conversions are mostly meant for parent consumers. Locally, defineEmits should just keep the exact event names for consistency.

You're right! The format conversion accommodates how the template is written. For event firing, we supposedly need to keep the event names consistent.

@nooooooom
Copy link
Author

The original intention of this PR is to keep defineEmits consistent in logic and type. Still, from the perspective of developing components, it will undoubtedly confuse the code specification of components.

I was going in the wrong direction for vuejs/language-tools#3197; we should enhance type hints on component usage in SFC.

@yyx990803
Copy link
Member

I think we can agree to close this one then? Regardless, thanks for your effort on the PR 👍

@yyx990803 yyx990803 closed this May 18, 2023
@nooooooom
Copy link
Author

nooooooom commented May 18, 2023

I think we can agree to close this one then? Regardless, thanks for your effort on the PR 👍

I think so too. Thank you for your patience and strict control over the code design.

I learned quite a bit about TypeScript on this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing typing for emmited event for generic components which define different casing style
2 participants