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 when using unspecified attributes #3496

Open
PurpleTape opened this issue Aug 22, 2023 · 12 comments
Open

TypeScript error when using unspecified attributes #3496

PurpleTape opened this issue Aug 22, 2023 · 12 comments

Comments

@PurpleTape
Copy link

Hello! I encountered the following situation: for vue components that wrap native html elements, all properties not specified in props automatically fall into attrs and can be applied to a native html element. However, volar indicates a typescript error, since properties not specified in props are missing from the type.

The problem occurs in the following cases:

  1. When using native html attributes on a component
  2. When a component wraps another component (WrapperComponent -> RootComponent). In this case, attributes not specified in props of WrapperComponent are passed to the attrs of RootComponent

Thus, the attributes are passed as intended, but the IDE displays a type error

Example:

// example.tsx
import { defineComponent } from 'vue'

const VButton = defineComponent(
  () => {
    return () => <button type="button">My button</button>
  },
  {
    props: {
      something: String
    }
  }
)

const usage = () => <VButton something="ok value" onClick={alert()} />
изображение

Info:
TypeScript Vue Plugin (Volar) v1.8.8

How can I eliminate the display of such errors?

Thanks!

@so1ve
Copy link
Member

so1ve commented Aug 22, 2023

Duplicate of #3301

@so1ve so1ve marked this as a duplicate of #3301 Aug 22, 2023
@so1ve so1ve closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2023
@so1ve so1ve reopened this Aug 22, 2023
@so1ve so1ve marked this as not a duplicate of #3301 Aug 22, 2023
@so1ve
Copy link
Member

so1ve commented Aug 22, 2023

Ah, I think this issue is more descriptive.

@PurpleTape
Copy link
Author

Is there any way to fix this problem?

@so1ve
Copy link
Member

so1ve commented Aug 24, 2023

@PurpleTape Maybe we need to fix types in vue core

@laterdayi
Copy link

@so1ve @johnsoncodehk Is there any progress now, This is the duplicate link https://play.vuejs。org/#eNrtV1tv2zYU/iuE9pIANnWxLMuZU3Tx2iCDtw7N+jIYCDSJltlIpEBSsYci/32HpO3Ikh04zYq+9CHO4b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Bi02v7btIwsMozRiYZaSgDwIzolxWle5bUHNFzRQtST/j5dsBHuDQzahUTTYmsuz/I/gKPiQAMnd6DTcuMB+I6NunH3wGT3TbMmu6bok67rX3R6geFEVJegsuaN4qScrLi hZEfKj08b5fmqQo+Oo3w1OiJrtc0iVJ7w/wP+sXrs7pT/g+6Mga+ atE5ERZ8bvbP8ga6J0QPml1sWnDEeFHInlR6xit2hV8aiDshp6J9sZ0mLL8L/lurQiT26R0oKYaRt/0Y/pM6k/hQrV3VXz8D+cpfao=XKStcPVhN3t3B6Di0rLlS/TCr8WXIGDzLztJpvBHLuXNjHlubBi02v7btIwsMozRiYZaSgDwIzolxWle5bUHNFzRQtST/j5dsBHuDQzahUTTYmsuz/I/gKPiQAMnd6DTcuMB+I6NunH3wGT3TbMmu6bo k67rX3R6geFEVJeGsuaN4qScrLihZEfKj08b5fmqQo+Oo3w1OiJrtc0iVJ7w/wP+sXrs7pT/g+6Mga+atE5ERZ8bvbP8ga6J0QPml1sWnDEeFHInlR6xit2hV8aiDshp6J9sZ0MLL8L/lurQ iT26R0oKYaRt/0Y/pM6k/hQrV3VXz8D+cpfao=XKStcPVhN3t3B6Di0rLlS/TCr8WXIGDzLztJpvBHLuXNjHlubBi02v7btIwsMozRiYZaSgDwIzolxWle5bUHNFzRQtST/j5dsBHuDQzahUTTYmsuz/I/gKPiQAMnd6DTcuMB+I6NunH3wGT3TbMmu6bo k67rX3R6geFEVJeGsuaN4qScrLihZEfKj08b5fmqQo+Oo3w1OiJrtc0iVJ7w/wP+sXrs7pT/g+6Mga+atE5ERZ8bvbP8ga6J0QPml1sWnDEeFHInlR6xit2hV8aiDshp6J9sZ0MLL8L/lurQ iT26R0oKYaRt/0Y/pM6k/hQrV3VXz8D+cpfao=

@huangbh1024
Copy link

It’s 2024 and this problem hasn’t been fixed yet?

@so1ve
Copy link
Member

so1ve commented Apr 12, 2024

Honestly you should add some types to props.

@rchl
Copy link
Collaborator

rchl commented Apr 12, 2024

@so1ve who are you replying to?

This is part of the standard Vue functionality so it's not reasonable to expect someone to create a workaround just for the editor integration. And also it's not always possible when relying on third-party components.

And also there are PRs (including yours) that seem to try to address it:

@so1ve
Copy link
Member

so1ve commented Apr 12, 2024

Those PRs are trying to address the usage of SFC, not TSX. IMO TSX should report those issues since there is no onClick property on VButton. Additionally, defineComponent is a vue core function, so it would be better if we handle the types in vue core.

@rchl
Copy link
Collaborator

rchl commented Apr 12, 2024

IMO TSX should report those issues since there is no onClick property on VButton.

Well, the Vue behavior is to treat unknown attributes as "fallthrough attributes". The VButton can't know up-front what one might want to pass as a fall-through attribute so it can't define them up-front.

Those PRs are trying to address the usage of SFC

Maybe you want to handle SFC and TSX separately (I have no opinion on that) but this is not a TSX-specific problem. It's equally valid for SFC:

Screenshot 2024-04-12 at 10 15 02


That said, I don't like this feature of Vue very much as if the Vue language tools would follow that logic (fix this issue basically) then mistyped prop names would no longer be reported as invalid and then there would be another group of people who would complain about that...

@so1ve
Copy link
Member

so1ve commented Apr 12, 2024

Well, the Vue behavior is to treat unknown attributes as "fallthrough attributes". The VButton can't know up-front what one might want to pass as a fall-through attribute so it can't define them up-front.

In my imagination defineComponent should add Record<string, any> to its props definition if inheritAttrs is not set or set to false.

I don't like this feature of Vue very much

+1 :)

@yinmingdi
Copy link

Here's a temporary workaround, guys. It might not be the most elegant solution, but it's effective.

import 'vue';

type EventHandler = (...args: any[]) => void;

declare module 'vue' {
  interface ComponentCustomProps {
    id?: string;
    role?: string;
    tabindex?: number;
    onClick?: EventHandler;
    onTouchend?: EventHandler;
    onTouchmove?: EventHandler;
    onTouchstart?: EventHandler;
    onTouchcancel?: EventHandler;
    onTouchmovePassive?: EventHandler;
    onTouchstartPassive?: EventHandler;
  }

  interface HTMLAttributes {
    onTouchmovePassive?: EventHandler;
    onTouchstartPassive?: EventHandler;
    onClickCapture?: EventHandler;
  }
}

You can find more context here at vant.
Thanks again to vant for being such an excellent component library.

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

No branches or pull requests

6 participants