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

Cannot type $emit on class style components #1097

Closed
sbarfurth opened this issue Mar 22, 2022 · 3 comments
Closed

Cannot type $emit on class style components #1097

sbarfurth opened this issue Mar 22, 2022 · 3 comments
Labels
question Further information is requested

Comments

@sbarfurth
Copy link

As mentioned in #1061 (comment) it seems it is currently not possible to type $emit using the method suggested in #750 (comment). The base interface uses a this return type and assigning void gives a TypeScript error that an arbitrary type can be passed.

Is there updated methodology for this? Or is this something that has fallen out of favor?

@johnsoncodehk
Copy link
Member

Hi @barfurth, I'm not really understand the problem, could you provide a complete code sample?

Just in case, make sure you have at least define a empty object type for $props to make to component is a valid JSX element. So it will not show red color in other components.

@sbarfurth
Copy link
Author

Certainly @johnsoncodehk, I added a component that is based entirely on #750 (comment) linked above.

Here is the component: https://github.com/barfurth/volar-no-types-repro/blob/main/components/HelloWorld.vue

The auto-completion works with this, but it does not compile. When you open the component the $emit part is marked as an error.

Property '$emit' in type 'HelloWorld' is not assignable to the same property in base type 'object & Record<never, any> & Vue'.
  Type '{ (e: "my-event-1"): void; (e: "my-event-2", arg1: string, arg2: number): void; }' is not assignable to type '(event: string, ...args: any[]) => this'.ts(2416)

There seems to be some problem with this assignment. The $props and the $scopedSlots work perfectly. This is related to the Vue interface definition of $emit I believe.

interface Vue {
  // ...
  $emit(event: string, ...args: any[]): this;
  // ...
}

@johnsoncodehk
Copy link
Member

@sbarfurth just have a quick look, you could change this code to avoid error, but I'm not sure will it effect other types.

  $emit!: {
-    (e: 'my-event-1'): void
-    (e: 'my-event-2', arg1: string, arg2: number): void
+    (e: 'my-event-1'): any
+    (e: 'my-event-2', arg1: string, arg2: number): any
  }

@johnsoncodehk johnsoncodehk added question Further information is requested and removed need info labels Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants