Skip to content

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

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

feat(types): infer slots type from SetupContext in defineComponent#14688
zhiyuanzmj wants to merge 8 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 typing for components, improving slot inference and editor autocomplete across options, setup, and functional component patterns.
  • Tests

    • Added unit tests to validate slot type inference (including default slot call signatures) to catch regressions and ensure correct typings.

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

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d5f3143c-04b7-4f19-aee9-9a6489a5cd6d

📥 Commits

Reviewing files that changed from the base of the PR and between b98f262 and 3c92199.

📒 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
✅ Files skipped from review due to trivial changes (1)
  • packages-private/dts-test/defineComponent.test-d.tsx
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/runtime-core/src/component.ts
  • packages/runtime-core/src/componentPublicInstance.ts
  • packages/runtime-core/src/apiDefineComponent.ts

📝 Walkthrough

Walkthrough

Broadened slot-related type generics to accept either SlotsType or a raw record 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 describe('infer slots from \SetupContext`')with fourdefineComponentcases (option API and functional variants) assertingthis.$slots.default/instance.$slots.defaultare notanyand that calling thedefaultslot with{ foo: 1 }type-checks. Also adjusted imports to includeEmitsOptions` in the initial vue type import and removed the later duplicate.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • jh-leong
  • johnsoncodehk

Poem

🐰 I hopped through types with nimble paws,
Widened slots without a pause.
Context whispers what props should be,
Tests confirm — hooray for me! 🎉

🚥 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: enhancing type inference for slots from SetupContext in defineComponent.
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: 3c92199

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.

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

@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
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.

2 participants