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

Passthrough events don't allow undefined handlers #3122

Closed
Tracked by #17282
KaelWD opened this issue May 4, 2023 · 4 comments · Fixed by #3322
Closed
Tracked by #17282

Passthrough events don't allow undefined handlers #3122

KaelWD opened this issue May 4, 2023 · 4 comments · Fixed by #3322
Labels
bug Something isn't working good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first

Comments

@KaelWD
Copy link

KaelWD commented May 4, 2023

Volar 1.6.4
Vue 3.3.0-beta.3

Related to #2700?

<template>
  <v-container>
    <Test @click="onClick" />
    <!--   ^^^^^ Type '(() => void) | undefined' is not assignable to type '(...payload: any[]) => void' -->
  </v-container>
</template>

<script setup lang="ts">
  import Test from './Test.vue'

  declare const onClick: (() => void) | undefined
</script>
// Test.vue
<template>
  <div></div>
</template>

<script setup lang="ts">
  defineEmits<{}>()
</script>

If the emit is defined it passes:

defineEmits<{
  (event: 'click', e: Event): void
}>()
@johnsoncodehk
Copy link
Member

This is a use case that requires unambiguous type behavior.

const Test = defineComponent({ emits: {} });
const emit = (new Test).$emit;
//    ^? (event: string, ...args: any[]) => void
declare const onClick: ConvertEmitToEvent<typeof emit, 'click'>;
//            ^? should it be `(...args: any[]) => void`?
//               or `((...args: any[]) => void) | undefined`?
//               or `((...args: any[]) => void) | undefined | null`?

type ConvertEmitToEvent<T> = ...

I think @click="onClick!" is a technically sound solution.

@KaelWD
Copy link
Author

KaelWD commented May 5, 2023

Is | undefined not possible? Vue adds undefined to events from defineEmits.

@xiaoxiangmoe
Copy link
Collaborator

temp solution: <Test @click="onClick?.()" /> 😂

@johnsoncodehk johnsoncodehk added bug Something isn't working good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first labels Jun 26, 2023
@johnsoncodehk
Copy link
Member

After some thought, it should accept undefined as long as it's optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good reproduction ✨ This issue provides a good reproduction, we will be able to investigate it first
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants