Skip to content

feat(types): infer slots type from SetupContext in defineComponent#14688

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

feat(types): infer slots type from SetupContext in defineComponent#14688
zhiyuanzmj wants to merge 10 commits intovuejs:mainfrom
zhiyuanzmj:setup-context-slots

Conversation

@zhiyuanzmj
Copy link
Copy Markdown
Member

@zhiyuanzmj zhiyuanzmj commented Apr 6, 2026

Just like a functional component.

const Baz = defineComponent(
  <T,>(
    props: { foo: T },
    ctx: {
      slots: { default?: (props: { foo: T }) => any }
    },
  ) => () => slots.default?.(props),
)
const baz = new Baz({ foo: 1 })
baz.$slots.default({ foo: 1 })

Summary by CodeRabbit

  • New Features

    • Enhanced TypeScript type inference for component slots. The framework now provides improved type support for slot definitions across different component patterns, enabling better type checking, autocomplete, and error detection in your IDE.
  • Tests

    • Added comprehensive test cases to validate type inference for component slots across different setup patterns.

Summary by CodeRabbit

  • New Features

    • Broader TypeScript slot typings for components, improving slot inference and editor autocomplete across options, setup, and functional/component patterns.
  • Tests

    • Added type-level tests to validate slot inference and default slot call signatures to prevent regressions.

mergeProps

Ensure event listeners that are explicitly null or undefined are
preserved during prop merging instead of being dropped.
Copilot AI review requested due to automatic review settings April 6, 2026 16:18
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 6, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Broadened slot-related type generics to accept either SlotsType or a raw Record<string, any> across defineComponent and instance types; added conditional normalization so SetupContext.slots and component public instance $slots infer correctly. Added d.ts tests covering four defineComponent slot-inference scenarios.

Changes

Cohort / File(s) Summary
Type Definition Updates
packages/runtime-core/src/apiDefineComponent.ts, packages/runtime-core/src/component.ts, packages/runtime-core/src/componentPublicInstance.ts
Widened slot generics to `SlotsType
Type Inference Tests
packages-private/dts-test/defineComponent.test-d.tsx
Added infer slots from \SetupContext`tests with fourdefineComponentscenarios (options API and functional/generic forms) assertingthis.$slots.default/instance.$slots.defaultare notanyand accept{ foo: number }. Adjusted imports to include EmitsOptionsin the primaryvue` import and removed the duplicate import.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

:cake: p2-nice-to-have

Suggested reviewers

  • jh-leong
  • johnsoncodehk

Poem

🐰 I hopped through types with nimble paws,
Widened slots without a pause.
Context now sings what props to send,
Tests gave nods from end to end. 🎉

🚥 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 summarizes the main change: enabling slot type inference from SetupContext in defineComponent, which is the core objective across all modified files.
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@14688
npm i https://pkg.pr.new/@vue/compiler-core@14688
yarn add https://pkg.pr.new/@vue/compiler-core@14688.tgz

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 14dfba3

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 updates runtime-core TypeScript typings to allow defineComponent / SetupContext slot typing to accept an “object slots” form (in addition to the existing SlotsType wrapper), and adds dts tests to cover the new inference path.

Changes:

  • Expand slot-related generics to accept SlotsType | Slots across SetupContext, defineComponent, and public instance typing.
  • Add conditional logic intended to map object-slot inputs back into SlotsType for $slots inference.
  • Add dts tests asserting $slots.default is not inferred as any when slots are provided via SetupContext.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
packages/runtime-core/src/componentPublicInstance.ts Allows mixin public instance slot generic to accept Slots and conditionally wraps it.
packages/runtime-core/src/component.ts Extends SetupContext slot generic and changes ctx.slots typing based on slot type form.
packages/runtime-core/src/apiDefineComponent.ts Updates defineComponent overload generics/imports to accept Slots in addition to SlotsType.
packages-private/dts-test/defineComponent.test-d.tsx Adds dts tests for slot inference flowing from SetupContext annotations.

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

Comment thread packages/runtime-core/src/component.ts
Comment thread packages/runtime-core/src/componentPublicInstance.ts
Comment thread packages-private/dts-test/defineComponent.test-d.tsx
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages-private/dts-test/defineComponent.test-d.tsx`:
- Around line 1178-1247: The tests reference the Vue type EmitsOptions but it
isn't imported; update the import list from 'vue' to include EmitsOptions so the
usages in the SetupContext generic parameters (seen in the Foo, Bar, and Qux
defineComponent blocks) resolve correctly; ensure the top-of-file import
alongside defineComponent, SetupContext, IsAny, etc., includes EmitsOptions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 486d3e1f-0113-474a-bc03-d0f5761d5da6

📥 Commits

Reviewing files that changed from the base of the PR and between 9a2eb53 and 56092ef.

📒 Files selected for processing (4)
  • packages-private/dts-test/defineComponent.test-d.tsx
  • packages/runtime-core/src/apiDefineComponent.ts
  • packages/runtime-core/src/component.ts
  • packages/runtime-core/src/componentPublicInstance.ts

Comment thread packages-private/dts-test/defineComponent.test-d.tsx
@zhiyuanzmj zhiyuanzmj changed the title feat(types): support object slots type for defineComponent feat(types): infer slots type from SetupContext in defineComponent Apr 7, 2026
@zhiyuanzmj
Copy link
Copy Markdown
Member Author

/ecosystem-ci run

@vuejs vuejs deleted a comment from zhiyuanzmj Apr 12, 2026
@vue-bot
Copy link
Copy Markdown
Contributor

vue-bot commented Apr 12, 2026

📝 Ran ecosystem CI: Open

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

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