Skip to content

fix(compiler-sfc): handle TSTypeAliasDeclaration in inferRuntimeType #13245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

linzhe141
Copy link
Contributor

@linzhe141 linzhe141 commented Apr 27, 2025

close #13240

Summary by CodeRabbit

  • Bug Fixes

    • Improved type inference to correctly handle type aliases, ensuring more accurate runtime type detection.
  • Tests

    • Added a new test case to validate prop definitions using TypeScript type aliases with default values.

Copy link

coderabbitai bot commented Apr 27, 2025

"""

Walkthrough

The update introduces a refined logic in the inferRuntimeType function within the type resolution module. Specifically, when encountering a TypeScript type reference, the function now checks if the resolved node is a type alias declaration. If so, it recursively infers the runtime type from the alias's underlying type annotation, rather than from the alias node itself. This adjustment ensures that type aliases, especially those involving generics or function types, are correctly unwrapped and their actual types are properly inferred at runtime.

Changes

File(s) Change Summary
packages/compiler-sfc/src/script/resolveType.ts Added a conditional branch in inferRuntimeType to recursively handle TSTypeAliasDeclaration when resolving type references, ensuring accurate runtime type inference for type aliases.
packages/compiler-sfc/tests/compileScript/defineProps.spec.ts Added a new test case "w/ TSTypeAliasDeclaration" to verify that generic function type aliases used in defineProps with defaults are correctly inferred as Function and compiled properly.

Sequence Diagram(s)

sequenceDiagram
    participant inferRuntimeType
    participant node
    participant resolveTypeReference
    participant resolvedNode

    inferRuntimeType->>node: Check node type
    alt node is TSTypeReference
        inferRuntimeType->>resolveTypeReference: Resolve type reference
        resolveTypeReference-->>inferRuntimeType: Return resolvedNode
        alt resolvedNode is TSTypeAliasDeclaration
            inferRuntimeType->>inferRuntimeType: Recurse on typeAnnotation of alias
        else
            inferRuntimeType->>inferRuntimeType: Recurse on resolvedNode
        end
    else
        inferRuntimeType->>inferRuntimeType: Handle other node types as before
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Correctly infer runtime types for function props using generic type aliases in defineProps (e.g., Fun2, Fun4) [#13240]
Ensure default function props are inferred as Function rather than null or boolean when using type aliases [#13240]

Poem

In the warren of types, a twisty new path,
Where aliases hid from the compiler's keen math,
Now bunnies can leap through the alias disguise,
And find the true type with their clever bright eyes!
No more will our functions turn null in the night—
The carrots of inference are crisp and just right!
🥕✨
"""


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between d6d16a5 and a6fd347.

⛔ Files ignored due to path filters (1)
  • packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineProps.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (1)
  • packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts (1)
packages/compiler-sfc/__tests__/utils.ts (1)
  • assertCode (27-42)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
  • GitHub Check: test / unit-test-windows
🔇 Additional comments (1)
packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts (1)

812-836: Good test for TypeScript type aliases in inferRuntimeType!

This new test case correctly validates the fix for handling TypeScript type alias declarations in prop types. It tests both a generic function type alias (FunFoo<O>) and a specialization of that alias (FunBar = FunFoo<number>), ensuring that:

  1. The compiler correctly resolves these to their runtime type (Function)
  2. The props are marked as optional (required: false)
  3. Default values are properly preserved in the output

The test follows the pattern of existing tests and addresses the issue described in the PR objectives.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB 38.1 kB 34.4 kB
vue.global.prod.js 158 kB 58.3 kB 51.9 kB

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.4 kB 18.1 kB 16.6 kB
createApp 54.4 kB 21.2 kB 19.4 kB
createSSRApp 58.6 kB 22.9 kB 20.9 kB
defineCustomElement 59.2 kB 22.7 kB 20.7 kB
overall 68.5 kB 26.3 kB 24 kB

Copy link

pkg-pr-new bot commented Apr 27, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/shared

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

vue

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

@vue/compat

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

@vue/server-renderer

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

commit: a6fd347

@linzhe141 linzhe141 marked this pull request as draft April 27, 2025 03:29
@linzhe141 linzhe141 closed this Apr 27, 2025
@linzhe141
Copy link
Contributor Author

image
There is no correct handling of this case.

@linzhe141 linzhe141 deleted the fix-compiler-defineProps branch April 27, 2025 07:04
@linzhe141 linzhe141 restored the fix-compiler-defineProps branch April 28, 2025 01:37
@linzhe141 linzhe141 reopened this Apr 28, 2025
@linzhe141 linzhe141 marked this pull request as ready for review April 28, 2025 01:59
@edison1105 edison1105 changed the title fix: handle TSTypeAliasDeclaration in inferRuntimeType fix(compiler-sfc): handle TSTypeAliasDeclaration in inferRuntimeType Apr 29, 2025
@edison1105 edison1105 added scope: sfc 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels Apr 29, 2025
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. scope: sfc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type inference failure in defineProps when using generic type aliases for function props
2 participants