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

fix(types): emit type in SetupContext #884

Merged
merged 1 commit into from Jan 16, 2022
Merged

Conversation

DerAlbertCom
Copy link
Contributor

If you define a component with emits, then you can type this.$emits

So with this, you get autocompletion and signature validation on this.$emit

  defineComponent({
    emits: {
      click: (n: number) => typeof n === 'number',
      input: (b: string) => b.length > 1,
    },
    created() {
      this.$emit('click', 1)
      this.$emit('input', 'foo')
    },
  })

but you don't get the same behavior in the setup function.

  defineComponent({
    emits: {
      click: (n: number) => typeof n === 'number',
      input: (b: string) => b.length > 1,
    },
    setup(props, { emit ) {
      emit('click', 1)
      emit('input', 'foo')
    },
  })

this will work, gives no errors, but also no auto completion or compilation safety with typescript.

This PR is will enable that, so that the emit property of the SetupContext has typed emits.

image

I don't know if there is a reason why this is not already the behavior of SetupContext, but it seems to be a good addition.

@DerAlbertCom DerAlbertCom changed the title Adding Emits typed SetupContext to setup function Adding Emits typed SetupContext to setup() function Jan 11, 2022
@antfu antfu changed the title Adding Emits typed SetupContext to setup() function fix(types): emit type in SetupContext Jan 16, 2022
@antfu antfu merged commit 5c35403 into vuejs:main Jan 16, 2022
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.

None yet

2 participants