Skip to content

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Feb 14, 2025

close #12872

Summary by CodeRabbit

  • New Features
    • Enhanced runtime type inference for props: now understands generic type aliases and correctly resolves unions and intersections to multiple runtime types.
  • Bug Fixes
    • More accurate resolution for keyof/index signatures in props (e.g., resolves to String/Number instead of Unknown).
    • Correct handling of complex alias types (e.g., intersections and unions) that previously fell back to Unknown.
    • Improved detection of wrapped/brand types to appropriate runtime counterparts (e.g., Boolean, Symbol, Number, Object).

Copy link

github-actions bot commented Feb 14, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.4 kB 34.6 kB
vue.global.prod.js 159 kB 58.5 kB 52.2 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 22.9 kB 20.9 kB
defineCustomElement 59.5 kB 22.8 kB 20.8 kB
overall 68.5 kB 26.4 kB 24.1 kB

Copy link

pkg-pr-new bot commented Feb 14, 2025

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@12876

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@12876

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@12876

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@12876

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@12876

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@12876

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@12876

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@12876

@vue/shared

npm i https://pkg.pr.new/@vue/shared@12876

vue

npm i https://pkg.pr.new/vue@12876

@vue/compat

npm i https://pkg.pr.new/@vue/compat@12876

commit: d57f2c7

@edison1105 edison1105 marked this pull request as ready for review February 14, 2025 09:55
@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: sfc 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Feb 14, 2025
@larsrickert
Copy link

@edison1105 Thanks for implementing this bug fix! Is there something blocking this PR from being merged and released?
It is currently affecting our component library usage because users can not use the boolean shorthand as expected

@edison1105 edison1105 changed the title feat(compiler-sfc): enhance type resolution with support for generic type parameters fix(compiler-sfc): enhance type resolution with support for generic type parameters Aug 21, 2025
Copy link

coderabbitai bot commented Aug 21, 2025

Walkthrough

Extends compiler-sfc’s runtime type inference to propagate and resolve generic type parameters across references, unions, and intersections. Updates tests to expect multiple concrete runtime types from union/intersection resolutions, including correct inference for generics.

Changes

Cohort / File(s) Summary
Type inference engine
packages/compiler-sfc/src/script/resolveType.ts
Threads typeParameters through inferRuntimeType, TSTypeReference, unions, intersections, and flattening. Adds mapping from alias type params to actual args, handles function-type aliases, and resolves identifier-based generic references via a type parameter context. Updates inferRuntimeType signature to include optional typeParameters.
Tests: type resolution
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Adjusts expectations: resolves keyof index signatures to String/Number; intersection/union cases now produce multiple runtime types; adds union test for Wrapped resolving to Boolean, Symbol, Number; renames a test to reflect intersection behavior.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as SFC Script
  participant CS as compileScript
  participant RT as inferRuntimeType
  participant TR as resolveTypeRef
  participant FL as flattenTypes

  Dev->>CS: defineProps<T>()
  CS->>RT: inferRuntimeType(node, scope, isKeyOf, typeParameters?)
  alt T is TSTypeReference
    RT->>TR: resolve referenced alias/decl
    alt Alias has its own type params
      RT->>RT: map alias params -> actual args (typeParameters)
    else Top-level type parameter
      RT->>RT: substitute from typeParameters
    end
    RT->>RT: recurse into alias typeAnnotation with mapped typeParameters
  else T is TSUnion/TSIntersection
    RT->>FL: flattenTypes(T, typeParameters)
    FL->>RT: infer per member with typeParameters
    RT->>RT: merge and filter UNKNOWN_TYPE (for intersections)
  else TSFunctionType alias
    RT-->>CS: ["Function"]
  end
  CS-->>Dev: props schema with concrete runtime types (e.g., Boolean)
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Assessment against linked issues

Objective Addressed Explanation
Infer primitive runtime prop types from generic types [#12872]
Ensure boolean casting works by having boolean inferred from generics [#12872]

Assessment against linked issues: Out-of-scope changes

(No out-of-scope functional changes identified.)

Possibly related PRs

Suggested reviewers

  • Doctor-wu

Poem

I nibbled on types in the clovery green,
Found Booleans in unions, a shimmering sheen.
Generics now mapped, no longer adrift—
Props sprout true leaves with a runtime lift.
Hop-hop hooray, intersections align—
Carrots for code that infers just fine! 🥕🐇

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 4a2953f and d57f2c7.

📒 Files selected for processing (2)
  • packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts (3 hunks)
  • packages/compiler-sfc/src/script/resolveType.ts (4 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/compiler-sfc/src/script/resolveType.ts (2)
packages/compiler-core/src/ast.ts (1)
  • Node (70-73)
packages/compiler-sfc/src/script/utils.ts (1)
  • UNKNOWN_TYPE (13-13)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test / e2e-test
🔇 Additional comments (8)
packages/compiler-sfc/src/script/resolveType.ts (5)

1498-1503: LGTM! The addition of the optional typeParameters parameter is well-designed.

The signature change to inferRuntimeType elegantly extends the function to support generic type resolution while maintaining backward compatibility through the optional parameter.


1592-1614: Good handling of TSTypeAliasDeclaration with function types and generics.

The code correctly:

  1. Preserves the existing behavior for function type aliases
  2. Maps type parameters when both the alias and reference have type parameters
  3. Recursively infers runtime types with the mapped parameters

1620-1628: Appropriate resolution of type parameters from context.

The code correctly resolves type references that match entries in the typeParameters map, enabling proper runtime type inference for generic type parameters.


1761-1769: Correct propagation of type parameters through union and intersection types.

The typeParameters are properly threaded through to flattenTypes for both union and intersection types. The intersection type continues to filter out UNKNOWN_TYPE as expected.


1835-1854: Well-implemented helper function with proper parameter threading.

The flattenTypes function correctly:

  1. Accepts the optional typeParameters parameter with a sensible default
  2. Propagates it through each type's inference call
  3. Maintains the existing deduplication logic
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts (3)

541-542: Test expectation correctly updated to reflect union/intersection type resolution.

The change from [UNKNOWN_TYPE] to ['String', 'Number'] properly validates that the keyof index signature now resolves to both String and Number runtime types.


752-763: Test properly verifies intersection type resolution.

The test correctly validates that Brand<string> (which is string & {}) now resolves to both String and Object runtime types instead of Unknown.


765-774: New test case validates the fix for issue #12872.

This test directly addresses the reported issue by verifying that Wrapped<boolean> (defined as T | symbol | number) correctly resolves to ['Boolean', 'Symbol', 'Number'], ensuring boolean casting will work properly at runtime.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch edison/fix/12872

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@edison1105
Copy link
Member Author

/ecosystem-ci run

@edison1105 edison1105 changed the title fix(compiler-sfc): enhance type resolution with support for generic type parameters fix(compiler-sfc): improve type inference for generic type aliases types Aug 21, 2025
@vue-bot
Copy link
Contributor

vue-bot commented Aug 21, 2025

📝 Ran ecosystem CI: Open

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

@edison1105 edison1105 merged commit d9dd628 into main Aug 21, 2025
16 checks passed
@edison1105 edison1105 deleted the edison/fix/12872 branch August 21, 2025 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: sfc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type-only props are unable to infer primitive runtime prop types from generic types, which breaks boolean casting
3 participants