Skip to content

feat(types): infer emits from SetupContext in defineComponent#14687

Open
zhiyuanzmj wants to merge 2 commits intovuejs:mainfrom
zhiyuanzmj:setup-context
Open

feat(types): infer emits from SetupContext in defineComponent#14687
zhiyuanzmj wants to merge 2 commits intovuejs:mainfrom
zhiyuanzmj:setup-context

Conversation

@zhiyuanzmj
Copy link
Copy Markdown
Member

@zhiyuanzmj zhiyuanzmj commented Apr 6, 2026

Remove the E extends any ? expression to correctly infer emits from SetupContext.

Don't worry, the #2362 issue has been resolved in this PR after 3.3.0-alpha-7.

Summary by CodeRabbit

  • Refactor

    • Simplified component setup context type system for improved type inference.
  • Tests

    • Added test coverage for component event typing in setup context.

Copilot AI review requested due to automatic review settings April 6, 2026 11:07
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 6, 2026

📝 Walkthrough

Walkthrough

This PR refactors the SetupContext type from a conditional type structure to a direct object type definition, eliminating the E extends any ? ... : never conditional logic while maintaining identical behavior. A test case is added to verify that SetupContext's emitted event typing is properly reflected in component props.

Changes

Cohort / File(s) Summary
SetupContext Type Refactoring
packages/runtime-core/src/component.ts
Simplified SetupContext type definition by removing the conditional E extends any distribution, converting it to a single straightforward object type with the same structural properties (attrs, slots, emit, expose).
SetupContext Type Inference Test
packages-private/dts-test/defineComponent.test-d.tsx
Added test case verifying that SetupContext with typed emits (SetupContext<['foo']>) correctly infers component props, specifically validating that emitted events are reflected as listener props (e.g., onFoo).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 hops with glee
The conditional chains have taken their flight,
SetupContext now gleams in pure, simple light!
No more E extends any to confuse the mind,
With tests to ensure all emissions align,
A cleaner type journey, how perfectly fine! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: refactoring SetupContext type to enable proper emit inference in defineComponent.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 105 kB 39.9 kB 35.8 kB
vue.global.prod.js 164 kB 59.9 kB 53.2 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 48.3 kB 18.8 kB 17.2 kB
createApp 56.5 kB 21.8 kB 20 kB
createSSRApp 60.7 kB 23.6 kB 21.5 kB
defineCustomElement 62.6 kB 23.8 kB 21.7 kB
overall 71 kB 27.2 kB 24.7 kB

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 6, 2026

Open in StackBlitz

@vue/compiler-core

pnpm add https://pkg.pr.new/@vue/compiler-core@14687
npm i https://pkg.pr.new/@vue/compiler-core@14687
yarn add https://pkg.pr.new/@vue/compiler-core@14687.tgz

@vue/compiler-dom

pnpm add https://pkg.pr.new/@vue/compiler-dom@14687
npm i https://pkg.pr.new/@vue/compiler-dom@14687
yarn add https://pkg.pr.new/@vue/compiler-dom@14687.tgz

@vue/compiler-sfc

pnpm add https://pkg.pr.new/@vue/compiler-sfc@14687
npm i https://pkg.pr.new/@vue/compiler-sfc@14687
yarn add https://pkg.pr.new/@vue/compiler-sfc@14687.tgz

@vue/compiler-ssr

pnpm add https://pkg.pr.new/@vue/compiler-ssr@14687
npm i https://pkg.pr.new/@vue/compiler-ssr@14687
yarn add https://pkg.pr.new/@vue/compiler-ssr@14687.tgz

@vue/reactivity

pnpm add https://pkg.pr.new/@vue/reactivity@14687
npm i https://pkg.pr.new/@vue/reactivity@14687
yarn add https://pkg.pr.new/@vue/reactivity@14687.tgz

@vue/runtime-core

pnpm add https://pkg.pr.new/@vue/runtime-core@14687
npm i https://pkg.pr.new/@vue/runtime-core@14687
yarn add https://pkg.pr.new/@vue/runtime-core@14687.tgz

@vue/runtime-dom

pnpm add https://pkg.pr.new/@vue/runtime-dom@14687
npm i https://pkg.pr.new/@vue/runtime-dom@14687
yarn add https://pkg.pr.new/@vue/runtime-dom@14687.tgz

@vue/server-renderer

pnpm add https://pkg.pr.new/@vue/server-renderer@14687
npm i https://pkg.pr.new/@vue/server-renderer@14687
yarn add https://pkg.pr.new/@vue/server-renderer@14687.tgz

@vue/shared

pnpm add https://pkg.pr.new/@vue/shared@14687
npm i https://pkg.pr.new/@vue/shared@14687
yarn add https://pkg.pr.new/@vue/shared@14687.tgz

vue

pnpm add https://pkg.pr.new/vue@14687
npm i https://pkg.pr.new/vue@14687
yarn add https://pkg.pr.new/vue@14687.tgz

@vue/compat

pnpm add https://pkg.pr.new/@vue/compat@14687
npm i https://pkg.pr.new/@vue/compat@14687
yarn add https://pkg.pr.new/@vue/compat@14687.tgz

commit: 0f93ce1

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts Vue runtime-core TypeScript types to improve inferring component emits from an explicitly-typed SetupContext parameter in setup().

Changes:

  • Simplifies SetupContext by removing the distributive conditional (E extends any ? ... : never).
  • Adds a dts test ensuring JSX listeners (e.g. onFoo) are inferred when setup() annotates ctx as SetupContext<['foo']>.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/runtime-core/src/component.ts Changes the exported SetupContext type definition (variance/inference behavior).
packages-private/dts-test/defineComponent.test-d.tsx Adds a dts regression test for emits inference from SetupContext.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zhiyuanzmj zhiyuanzmj changed the title fix(types): infer emits from SetupContext feat(types): infer emits from SetupContext Apr 6, 2026
@zhiyuanzmj zhiyuanzmj changed the title feat(types): infer emits from SetupContext feat(types): infer emits from SetupContext in defineComponent Apr 7, 2026
@zhiyuanzmj
Copy link
Copy Markdown
Member Author

/ecosystem-ci run

@vue-bot
Copy link
Copy Markdown
Contributor

vue-bot commented Apr 10, 2026

📝 Ran ecosystem CI: Open

suite result latest scheduled
nuxt success failure
radix-vue success success
test-utils success success
language-tools failure failure
pinia success success
primevue success success
vant success success
vueuse success success
vue-simple-compiler success success
vuetify success failure
vitepress success success
vue-i18n success success
quasar success failure
vite-plugin-vue success success
vue-macros success success
router success failure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants