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

Vue 3 + Typescript: "setup" function breaks emits typing #5343

Closed
iliubinskii opened this issue Jan 28, 2022 · 3 comments
Closed

Vue 3 + Typescript: "setup" function breaks emits typing #5343

iliubinskii opened this issue Jan 28, 2022 · 3 comments

Comments

@iliubinskii
Copy link

Version

3.0.0

Reproduction link

github.com

Steps to reproduce

  1. Clone reproduction repository
  2. Run "validate" script: npm run validate
    OR
    open Sample1.ts and Sample2.ts in Visual Studio Code.

What is expected?

I expect to see two errors:

One from

emit("wrong-event-name");

in Sample1.ts

And another one from

this.$emit("wrong-event-name");

in Sample2.ts

What is actually happening?

I see only one error - from Sample2.ts


In Sample2 defineComponent correctly determines emits type as:

{
    event(): boolean;
}

image

In Sample1 it uses EmitsOptions type because of _props and { emit } args.

image

If you remove args, emits type is determined correctly, but in this case you will not have access to emit function which is not suitable.

image

@posva posva transferred this issue from vuejs/vue Jan 29, 2022
@LinusBorg
Copy link
Member

LinusBorg commented Jan 29, 2022

I think this is about the same TS limitation we have with props validator functions: they either need to be arrow functions or have this typed to undefined explicitly. Both of these work fine:

emits: {
   // arrow function
   event: (): boolean => { return true; }
  // explicit this
  event(this: undefined): boolean { return true; }
},

Bildschirmfoto 2022-01-29 um 13 05 02

I'll submit a change to the docs adding a similar note like we have for props validators and will close the issue afterwards.

We should also consider to add an eslint rule for this (in case it doesn't exist yet).

@iliubinskii
Copy link
Author

Thx for the help.

Both fixes worked.

ESLint rule would be great. It should probably replace this ESLint core rule - "object-shorthand".

@posva
Copy link
Member

posva commented Jan 29, 2022

Can you open an issue on vuejs eslint repo if there isn't one yet?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants