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

Impossible to override native events #116

Closed
sirxemic opened this issue Mar 27, 2021 · 9 comments
Closed

Impossible to override native events #116

sirxemic opened this issue Mar 27, 2021 · 9 comments
Labels
enhancement New feature or request

Comments

@sirxemic
Copy link

Hello!

It seems like it's impossible to override native events. Simple example:

/* Foo.vue */
<template>
  <div />
</template>

<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
  emits: {
    foobar: (a: string) => true,
    click: (a: string) => true,
    submit: (a: string) => true,
    load: (a: string) => true
  }
})
</script>

and another component using Foo.vue:

<template>
  <Foo
    @foobar="handler"
    @click="handler"
    @submit="handler"
    @load="handler"
  />
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import Foo from './Foo.vue'

export default defineComponent({
  components: {
    Foo
  },

  methods: {
    handler (k: string) {
      console.log(k)
    }
  }
})
</script>

Volar incorrectly gives the error Type '(k: string) => void is not assignable to type '(payload: Event) => void'. for all event bindings, except foobar, the non-native one.

I don't think this is desirable behaviour. Vue works exactly how I expect it to work, but Volar does not.

@johnsoncodehk
Copy link
Member

johnsoncodehk commented Mar 27, 2021

Try this:

import { defineComponent, PropType } from 'vue'

export default defineComponent({
  props: {
    onClick: Function as PropType<(a: string) => void>,
    ...
  },
})

See: #41 (comment)

@johnsoncodehk johnsoncodehk added the question Further information is requested label Mar 27, 2021
@sirxemic
Copy link
Author

Hmm yes that works, but still, shouldn't we be able to just define the emits and make it work out of the box? I feel like what you're suggesting is a workaround. Am I wrong for thinking this is a bug? If so, why?

@johnsoncodehk
Copy link
Member

Vue document does not clearly point out events priority from props and emits, so now only allow override native events with same props to avoid confusion. It may change in the future.

@sirxemic
Copy link
Author

The documentation literally states:

When a native event (e.g., click) is defined in the emits option, the component event will be used instead of a native event listener.

So given that, I think it's clear that Volar should use the component event instead of the native event in these cases.

@johnsoncodehk
Copy link
Member

Thanks to point this out!

@johnsoncodehk johnsoncodehk reopened this Mar 28, 2021
@johnsoncodehk johnsoncodehk added enhancement New feature or request and removed question Further information is requested labels Mar 28, 2021
@johnsoncodehk
Copy link
Member

Fixed in 0.22.24.

@sirxemic
Copy link
Author

That was quick, thank you!

@MortalDo
Copy link

MortalDo commented Mar 9, 2022

v0.32.1
when I use ant-design-vue
<a-menu @click="onClick">
volar: Type 'MenuClickEventHandler' is not assignable to type '(payload: MouseEvent) => void'.
Types of parameters 'info' and 'payload' are incompatible.

It looks like a similar problem

@MortalDo
Copy link

v0.32.1 still get this:
Type '(e: Number) => void' is not assignable to type '(payload: MouseEvent) => void'.
Types of parameters 'e' and 'payload' are incompatible.

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

No branches or pull requests

3 participants