fix(compiler-sfc): infer Vue ref wrapper types when source is unresolvable#14758
fix(compiler-sfc): infer Vue ref wrapper types when source is unresolvable#14758ashishkr96 wants to merge 1 commit intovuejs:mainfrom
Conversation
…urce is unresolvable When a `MaybeRef<T[]>` (or similar Vue ref wrapper) appears in a `defineProps` union and the `vue` import cannot be resolved by the SFC compiler (e.g. when the type lives in a separately-built package), `resolveTypeReference` throws and the whole branch falls back to `UNKNOWN_TYPE`. After UNKNOWN filtering, valid runtime types like `Array` are silently dropped from the generated runtime props, producing incorrect `Invalid prop` warnings at runtime. Catch the resolution failure locally so the built-in name handling still runs, and add fallbacks for the well-known Vue wrapper types (`Ref`, `ShallowRef`, `ComputedRef`, `WritableComputedRef`, `MaybeRef`, `MaybeRefOrGetter`). close vuejs#14729
📝 WalkthroughWalkthroughThe changes improve runtime props type inference in Vue's SFC compiler by adding error tolerance to type resolution and explicit type mappings for Vue ref wrapper types ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts (1)
806-860: Consider relocating these tests out oftype alias declaration.Both new cases primarily exercise ref-wrapper inference when the type source is unresolvable; the second one doesn’t involve a type alias at all, and the first is really about interface inheritance. They’d read more naturally under their own
describe('ref wrapper inference', ...)(or inexternal type importsnext to the unresolvable-extends test). Purely organizational — behavior and assertions look correct.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts` around lines 806 - 860, The two tests ("MaybeRef wrapped array type in interface inheritance chain" and "Vue ref wrapper types in union are inferred when source is unresolvable") are misplaced under "type alias declaration" and should be moved to a more appropriate suite that focuses on ref-wrapper inference or external type imports; relocate the tests (the blocks referencing MaybeRef, Ref, ISelectable, XSelectProps, defineProps and resolve) into a new or existing describe('ref wrapper inference', ...) (or next to the unresolvable-extends test) so their intent is clearer, ensuring you keep the existing test bodies and expectations unchanged while updating the test file structure only.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts`:
- Around line 806-860: The two tests ("MaybeRef wrapped array type in interface
inheritance chain" and "Vue ref wrapper types in union are inferred when source
is unresolvable") are misplaced under "type alias declaration" and should be
moved to a more appropriate suite that focuses on ref-wrapper inference or
external type imports; relocate the tests (the blocks referencing MaybeRef, Ref,
ISelectable, XSelectProps, defineProps and resolve) into a new or existing
describe('ref wrapper inference', ...) (or next to the unresolvable-extends
test) so their intent is clearer, ensuring you keep the existing test bodies and
expectations unchanged while updating the test file structure only.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: ac56e932-bff8-4c07-a4d3-f49e5e972792
📒 Files selected for processing (2)
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.tspackages/compiler-sfc/src/script/resolveType.ts
|
@edison1105 Can you please review. |
@vue/compiler-core
@vue/compiler-dom
@vue/compiler-sfc
@vue/compiler-ssr
@vue/reactivity
@vue/runtime-core
@vue/runtime-dom
@vue/server-renderer
@vue/shared
vue
@vue/compat
commit: |
Size ReportBundles
Usages
|
|
/ecosystem-ci run |
|
Thanks, LGTM~ |
|
📝 Ran ecosystem CI: Open
|
Close #14729
Summary
MaybeRef<T[]>(or any other Vue ref-wrapper type) appears in adefinePropsunion and thevueimport cannot be resolved by the SFC compiler — for example when the type lives in a separately-built package consumed by another app —resolveTypeReferencethrows insideinferRuntimeType. The throw is caught at the function's top-leveltry/catchand the entire branch collapses toUNKNOWN_TYPE. After theUNKNOWN_TYPEfiltering indefineProps.ts, valid runtime types (e.g.Array) are silently dropped from the generated runtime props, producingInvalid prop: type check failedwarnings at runtime even though the value is valid per the source TS type.TSTypeReferencecase so the existing built-in name handling still gets a chance to run.Ref,ShallowRef,ComputedRef,WritableComputedRef(each →Object),MaybeRef<T>(→Object | inferRuntimeType(T)) andMaybeRefOrGetter<T>(→Object | Function | inferRuntimeType(T)). This mirrors the existing handling forPromise,Date,Map, etc.Test plan
MaybeRef wrapped array type in interface inheritance chainmirroring the issue's exact code shape — confirmsArrayis now present for theoptionsprop.Vue ref wrapper types in union are inferred when source is unresolvablecovering all six wrapper types.packages/compiler-sfctest suite passes (455 tests).Summary by CodeRabbit
Bug Fixes
Tests