Skip to content

Custom event auto complete #2558

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

Merged
merged 13 commits into from
Jan 21, 2021

Conversation

sapphi-red
Copy link
Contributor

@sapphi-red sapphi-red commented Dec 11, 2020

This PR implements custom event auto complete.

Obtains data from

emits: {
  foo: () => true,
}

and

emits: ['foo']

.

image

Validations are not implemented.

close #2392

Copy link
Member

@yoyo930021 yoyo930021 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We miss a type check feature in event.
Location of the related code:

props: { ${propTypeStrings.join(', ')} }

on: ComponentListeners<T>;

You can try to study it or I do it.

Thanks for your PR.
We originally expected to change to a full type implementation.
But I think your implementation is perfect for the transition period.

@sapphi-red
Copy link
Contributor Author

sapphi-red commented Dec 13, 2020

I added features below!

  • type check
  • @Emit support
    • If there is multiple @Emit with same emit name, it infers that it returns the union type of those return type.
      • For example if @Emit('foo') foo() {return 1} and @Emit('foo') foo2() {return true} exists, @foo becomes (arg: number | boolean) => any.
    • If emits option is set also, it will not obtain info from @Emit.

Copy link
Member

@yoyo930021 yoyo930021 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I need some time for test.
Thanks for your help.

@bissolli
Copy link

bissolli commented Jan 3, 2021

It doesn't give us type hints on what is coming from the event right? I've trying to find a good way to add a typed event but couldn't get anywhere close to a complete solution!

Found this very nice package, but I think it's not supported by Vetur - in the component where you use the event, it's not able to recognize the payload of the event!

Is there any good solution for that?

@sapphi-red
Copy link
Contributor Author

sapphi-red commented Jan 3, 2021

How about using emits option? See https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md#type-inference.
This PR obtains type data from this.

@bissolli
Copy link

bissolli commented Jan 4, 2021

How about using emits option? See https://github.com/vuejs/rfcs/blob/master/active-rfcs/0030-emits-option.md#type-inference.
This PR obtains type data from this.

If I want to use it only for typing purpose I would need only to return true, right?
Any guesses on how long this PR will take to merge?

const Foo = defineComponent({
  emits: {
    submit: (payload: { email: string; password: string }) => true
  },

  methods: {
    onSubmit() {
      this.$emit('submit', {
        email: '[email protected]',
        password: 123 // Type error!
      })

      this.$emit('non-declared-event') // Type error!
    }
  }
})

Also I am not very familiar with VSCode extensions, is there a way to use you PR to test it out?

@yoyo930021
Copy link
Member

Also I am not very familiar with VSCode extensions, is there a way to use you PR to test it out?

https://github.com/vuejs/vetur/wiki/Developing-Language-Server-Features

@yoyo930021
Copy link
Member

LGTM, I will merge it when weekend.

The transforming template logic is changed in this PR.
@ktsn @octref can read it when you have time.

@sapphi-red
Copy link
Contributor Author

I fixed the test with class components with @Options since it was changed forVue 3.
Also added the test with property decorator with Vue 2.

@yoyo930021 yoyo930021 merged commit 75805f1 into vuejs:master Jan 21, 2021
@sapphi-red sapphi-red deleted the custom-event-auto-complete branch January 21, 2021 18:31
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

Successfully merging this pull request may close these issues.

Template autocomplete for custom events
3 participants