Skip to content

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

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

feat(types): infer emits from SetupContext in defineComponent#14687
zhiyuanzmj wants to merge 3 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 the component setup context type to a single, consistent shape, improving type inference behavior.
  • Tests

    • Added type-level tests verifying that component emits declared in setup context are correctly reflected as TSX event props.

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ed50f2d4-23b8-4eef-8237-3ece1e6b92da

📥 Commits

Reviewing files that changed from the base of the PR and between 97e569a and a9253f0.

📒 Files selected for processing (1)
  • packages-private/dts-test/defineComponent.test-d.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages-private/dts-test/defineComponent.test-d.tsx

📝 Walkthrough

Walkthrough

Refactored SetupContext from a conditional/distributive type to a single object type and added a type-test that asserts emits declared via SetupContext<['foo']> are reflected as onFoo TSX listener props.

Changes

Cohort / File(s) Summary
SetupContext Type Refactoring
packages/runtime-core/src/component.ts
Replaced the conditional/distributive SetupContext<E = ..., S = ...> = E extends any ? { ... } : never with a direct object type SetupContext<E = ..., S = ...> = { attrs; slots; emit: EmitFn<E>; expose(...) }.
Type Tests for Emits Inference
packages-private/dts-test/defineComponent.test-d.tsx
Added a describe('infer emits from SetupContext', ...) test defining Foo and Bar components whose setup uses SetupContext<['foo']> and asserting TSX usage accepts onFoo listener props.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰
I nibbled through types to make them neat,
Flattened a branch where conditionals meet.
Emits now hop out, tidy and spry,
TSX hears onFoo — a joyful cry! 🎉

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: refactoring SetupContext to infer emits from the type parameter, which is the core modification across both the type definition and test additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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: a9253f0

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.

Comment thread packages/runtime-core/src/component.ts
@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 12, 2026

📝 Ran ecosystem CI: Open

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

@vuejs vuejs deleted a comment from zhiyuanzmj Apr 12, 2026
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.

4 participants