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

TypeScript error for @click - Type '() => void' is not assignable to type 'undefined' #464

Closed
ttntm opened this issue Sep 7, 2021 · 25 comments
Labels
duplicate This issue or pull request already exists

Comments

@ttntm
Copy link

ttntm commented Sep 7, 2021

Updated to VS Code 1.60 (TypeScript 4.4) today and started seeing these TS errors in my SFC's:

<script setup lang="ts">
  ...
  const onFilterBtnClick = () => windowOpen.value === 0 ? showWindow(3) : showWindow(0)
  ...
</script>

<template>
  ...
    <ButtonFilter :window="windowOpen" @click="onFilterBtnClick" />
  ...
</template>

There were no issues when I worked on the project last week.

Full component: https://github.com/ttntm/recept0r-ts/blob/main/src/views/Home.vue

This TS error happens to any @click attributes that are attached to components with props defined via TypeScript (as described here: https://v3.vuejs.org/api/sfc-script-setup.html#typescript-only-features):

  • @click for components with TS props: (property) click: undefined
  • @click for components without TS props: (property) click: ((...args: any[]) => any) | undefined

...copying that type definition into the button component's defineProps doesn't make any difference; any type for property click seems to be ignored.

@johnsoncodehk
Copy link
Member

I can't reproduce this problem, please try update your deps.

@johnsoncodehk
Copy link
Member

johnsoncodehk commented Sep 7, 2021

Duplicate of #460

@johnsoncodehk johnsoncodehk marked this as a duplicate of #460 Sep 7, 2021
@johnsoncodehk johnsoncodehk added the duplicate This issue or pull request already exists label Sep 7, 2021
@ttntm
Copy link
Author

ttntm commented Sep 7, 2021

Thanks, I didn't find #460 before opening this one, should have looked more carefully.

Edit: removed to avoid misinformation.

@johnsoncodehk
Copy link
Member

johnsoncodehk commented Sep 7, 2021

No click event should be (property) click: ((payload: MouseEvent) => void) | undefined by default, if it not it's a bug.
image

@ttntm
Copy link
Author

ttntm commented Sep 7, 2021

No click event should be (property) click: ((payload: MouseEvent) => void) | undefined by default, if it not it's a bug.

This is what I get "by default" after running npm update:

Code_kLFkOodNgH

vue@3.2.9
vite@2.5.4
vite plugin-vue@1.6.1

@johnsoncodehk
Copy link
Member

Have you try delete package-lock.json / yarn.lock + node_modules and reinstall it?

@ttntm
Copy link
Author

ttntm commented Sep 7, 2021

Not yet, will give it another try tomorrow and test on another machine too.

Will report back with additional info.

Thanks!

@ttntm
Copy link
Author

ttntm commented Sep 8, 2021

Good morning, just deleted node_modules and package-lock.json, reinstalled and @click types are working correctly again.

Still no idea what broke it, might look into it some more if it ever happens again.

Thanks for your time and tips @johnsoncodehk, really appreciate it!

@vincerubinetti
Copy link

I found that reinstalling node_modules didn't work for me.

Here's something strange I noticed though: I have a custom component that I'm using a @click.stop attribute on, with no ="" or anything, and it's giving me the error. But oddly, if I change the tag name from my custom component to a built-in HTML component, e.g. <button>, the error goes away. Another remedy is to just specify a blank attribute (which I guess Vue treats as undefined?) like this: <CustomComponent @click.stop=""/>

@johnsoncodehk
Copy link
Member

@vincerubinetti fixed by 94d1546.

@omarmfs98
Copy link

make sure you don't have this extension installed in visual studio code: Vue Language Features (Fly), uninstalling it removed that underline

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=Vue.volar

@ldsenow
Copy link

ldsenow commented Oct 11, 2022

This issue appears again on v1.0.3. This version has a few breaking changes e.g. this issue and also kebab-case prop

@jbaubree
Copy link

@ldsenow new issue was created for that #1985

@xieyuheng
Copy link

Here is a minimal vue component for you to reproduce this problem:

"vue-tsc": "^1.0.7"

Error:

src/components/Link.vue:8:6 - error TS2322: Type '() => void' is not assignable to type 'MouseEvent'.

8     @click="
       ~~~~~
<script setup lang="ts">
// We can not use <RouterLink> in headlessui:
// - https://github.com/tailwindlabs/headlessui/issues/143
</script>

<template>
  <span
    @click="
      () => {
        if (typeof $attrs.href === 'string') {
          if ($attrs.mode === 'replace') {
            $router.replace($attrs.href)
          } else {
            $router.push($attrs.href)
          }
        }
      }
    "
    ><a class="cursor-pointer"><slot /></a
  ></span>
</template>

@joker77877
Copy link

Here is a minimal vue component for you to reproduce this problem:

"vue-tsc": "^1.0.7"

Error:

src/components/Link.vue:8:6 - error TS2322: Type '() => void' is not assignable to type 'MouseEvent'.

8     @click="
       ~~~~~
<script setup lang="ts">
// We can not use <RouterLink> in headlessui:
// - https://github.com/tailwindlabs/headlessui/issues/143
</script>

<template>
  <span
    @click="
      () => {
        if (typeof $attrs.href === 'string') {
          if ($attrs.mode === 'replace') {
            $router.replace($attrs.href)
          } else {
            $router.push($attrs.href)
          }
        }
      }
    "
    ><a class="cursor-pointer"><slot /></a
  ></span>
</template>

I encountered the same problem in the Nuxt3 project.

@harjim
Copy link

harjim commented Oct 13, 2022

when i run build.

image

"@types/node": "^18.8.5",
"@vitejs/plugin-vue": "^3.1.2",
"typescript": "^4.8.4",
"vite": "^3.1.7",
"vue": "^3.2.40",
"vue-tsc": "^1.0.7"

@ldsenow
Copy link

ldsenow commented Oct 13, 2022

Downgrade to types/18.8.0 fixed the issue for me

@harjim
Copy link

harjim commented Oct 13, 2022

Downgrade to types/18.8.0 fixed the issue for me

yeah, I just solved

@Amixx
Copy link

Amixx commented Oct 13, 2022

Downgrade to types/18.8.0 fixed the issue for me

thanks a lot, this resolved this issue for me!

@Utitofon-Udoekong
Copy link

Downgrade to types/18.8.0 fixed the issue for me

Sorry newbie here. How do I downgrade this in nuxt 3?

@joker77877
Copy link

Downgrade to types/18.8.0 fixed the issue for me

Sorry newbie here. How do I downgrade this in nuxt 3?

npm i @types/node@18.8.0

@xieyuheng
Copy link

This issue will be fixed after we merge: vuejs/core#6855

@frederikheld
Copy link

frederikheld commented Oct 17, 2022

@omarmfs98 your solution is like little kids playing hide and seek: If I cover my eyes, nobody can see me.

Apparently the problem is not within Volar, so uninstalling Volar will not make it go away, you will just not see it anymore - like a lot of other useful stuff Volar is showing you.

@microHoffman
Copy link

Any updates on this? I have updated dependencies and I still see this in my ts errors:

Type '(e?: MouseEvent) => Promise<void | NavigationFailure>' is not assignable to type '(payload: KeyboardEvent) => void'.
  Types of parameters 'e' and 'payload' are incompatible.
    Type 'KeyboardEvent' is missing the following properties from type 'MouseEvent': button, buttons, clientX, clientY, and 12 more.

Template:

        <router-link
          v-for="(navigationItem, index) of navigationItems"
          :key="index"
          v-slot="{ isActive, navigate }"
          ...>
          <div
              @click="navigate"
              <!-- following line creates the error above -->
              @keypress.enter="navigate">

@rbluethl
Copy link

rbluethl commented Feb 5, 2023

Downgrade to types/18.8.0 fixed the issue for me

Any news on this? I just downgraded to 18.8.0 which solved it for me for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests