Skip to content

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Sep 4, 2025

only for the strict pattern {[K in keyof T]: T[K]}

close #13847

Summary by CodeRabbit

  • New Features
    • Improved runtime type inference for certain TypeScript mapped types used in component props, enabling more accurate runtime prop type generation and better compatibility with advanced TS patterns.
  • Tests
    • Added test coverage for mapped types with indexed access in defineProps to ensure correct prop type resolution.

…ith indexed access

only for the strict pattern {[K in keyof T]: T[K]}

close #13847
Copy link

coderabbitai bot commented Sep 4, 2025

Walkthrough

Adds runtime type inference for a specific TS mapped type pattern in inferRuntimeType and introduces tests validating mapped type with indexed access resolution in defineProps. No public API changes.

Changes

Cohort / File(s) Summary
SFC compiler runtime type inference
packages/compiler-sfc/src/script/resolveType.ts
Extends inferRuntimeType to handle a constrained TSMappedType pattern using TSIndexedAccessType and keyof constraints, resolving to the referenced target type; otherwise returns UNKNOWN_TYPE.
CompileScript type resolution tests
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Adds test “TSMappedType with indexed access” asserting defineProps<{ placement?: Placement }>() resolves placement to String and Object; test content appears inserted twice.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Dev as SFC Type Annotations
  participant SFC as compiler-sfc
  participant RT as inferRuntimeType
  participant MT as TSMappedType Branch

  Dev->>SFC: defineProps<{ placement?: Placement }>()
  SFC->>RT: inferRuntimeType(node, typeParameters)
  alt Node is TSMappedType
    RT->>MT: Validate pattern ({ [K in keyof T]: T[K] })
    alt Pattern and references match
      MT-->>RT: Resolve target from typeParameters
      RT-->>SFC: Inferred runtime type
    else Unsupported/invalid pattern
      MT-->>RT: UNKNOWN_TYPE
      RT-->>SFC: UNKNOWN_TYPE
    end
  else Other node kinds
    RT-->>SFC: Existing inference paths
  end
  SFC-->>Dev: Prop runtime type(s) for validation
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Assessment against linked issues

Objective Addressed Explanation
Resolve mapped type with indexed access used to construct Placement for prop validation (#13847)
Ensure default value validation accepts valid imported union-like Placement values (e.g., "right-end") (#13847)
Prevent build error in defineProps default when value conforms to imported Placement type (#13847)

Assessment against linked issues: Out-of-scope changes

[No out-of-scope functional code changes detected.]

Possibly related PRs

Suggested labels

ready to merge, scope: sfc, :hammer: p3-minor-bug, regression

Poem

I nibbled types like clover leaves, so sweet,
A mapped-field maze beneath my feet.
I sniffed the keys, found T by name—
Now “right-end” hops without a blame.
With whiskers twitching, tests in tow,
The props align—onwards we go! 🐇✨

✨ 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/TSMappedType

🪧 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 or @coderabbit 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.

Copy link

github-actions bot commented Sep 4, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.5 kB 34.6 kB
vue.global.prod.js 159 kB 58.6 kB 52.1 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.6 kB 21.3 kB 19.4 kB
createSSRApp 58.9 kB 23 kB 21 kB
defineCustomElement 59.6 kB 22.8 kB 20.9 kB
overall 68.8 kB 26.4 kB 24.2 kB

Copy link

pkg-pr-new bot commented Sep 4, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 99bbec5

@edison1105 edison1105 marked this pull request as ready for review September 4, 2025 14:55
Copy link

@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: 0

🧹 Nitpick comments (3)
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts (1)

734-748: Good regression coverage; add an import-based case mirroring #13847.

This validates the mapped-identity path via inline types. To guard against future regressions that involve imported aliases (the original failure mode), add a companion test that defines Prettify/Placement in an external file and imports it in the SFC before calling defineProps. This ensures cross-file resolution and caching paths participate in the fix.

I can draft the import-based test using the local files map used elsewhere in this suite.

packages/compiler-sfc/src/script/resolveType.ts (2)

1819-1819: Preserve isKeyOf when delegating to the target type.

If TSMappedType appears under a keyof operator, the current call drops the isKeyOf context, which can change the inferred runtime type. Propagate the flag.

-                return inferRuntimeType(ctx, targetType, scope)
+                return inferRuntimeType(ctx, targetType, scope, isKeyOf)

1784-1824: Optional: accept trivially-equivalent variants.

If you want slightly broader compatibility without losing the “strict pattern” intent, consider also accepting readonly/optional modifiers that don’t affect value inference (e.g., { readonly [K in keyof T]-?: T[K] }). This would remain conservative, but reduce surprises from common utility aliases.

I can follow up with a tiny helper (e.g., isIdentityMappedType(node)) and a couple of focused tests if desired.

📜 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 75220c7 and 99bbec5.

📒 Files selected for processing (2)
  • packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts (1 hunks)
  • packages/compiler-sfc/src/script/resolveType.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/compiler-sfc/src/script/resolveType.ts (1)
packages/compiler-sfc/src/script/utils.ts (1)
  • UNKNOWN_TYPE (13-13)
🔇 Additional comments (1)
packages/compiler-sfc/src/script/resolveType.ts (1)

1784-1824: Mapped-identity recognition LGTM for { [K in keyof T]: T[K] }.

The structural checks are tight and limited to the declared scope; this should correctly restore string inference for Prettify<T> without widening unrelated mapped types.

@edison1105 edison1105 marked this pull request as draft September 5, 2025 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression in prop type checking of imported type literal unions
1 participant