Backtestsvuerun-20261013-093701#15096
defineModel: named model with PropType + factory default infers wrong generic (regression from #14968)
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Patch A fixes the root cause. It wraps the default validator in `NoInfer`, so the `default` factory can no longer be an inference site for `T`. `T` is then inferred from `PropOptions<T>`, which includes `type: PropType<T>` and still lets a plain `default` drive inference when no `type` is given. The default is only checked against the resolved `T`. That is exactly the precedence the issue asks for. Patch B only swaps the order of the intersection members. That relies on TypeScript's candidate ordering and common-supertype selection during inference. Even if it happens to work for `() => []`, where `never[]` widens to `string[]`, it is fragile and implicit: the conflicting `() => never[]`-style candidate from the `InferDefault` union still exists, and the outcome depends on undocumented inference order. On tests, Patch A adds much broader coverage: - named and unnamed forms with runtime `type` plus factory default (array, object, number, function) - inferred-from-default cases with no `type`, guarding against regressions in plain default inference - several negative cases: a mismatched factory, a raw array default with `PropType`, and an unnamed mismatch Patch B tests only the named and unnamed array case plus one negative case. One caveat for A: `NoInfer` requires TS 5.4+ in emitted declarations. Even so, the approach is explicit, commented, and more robust. A is the better fix.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
12m 6s
Wall-clock time in the container
Full test suite
No new failures
1,736 passed · 1 skipped · 1,737 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
56vs 26 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.5.39
Link to minimal reproduction
Steps to reproduce
In the reproduction, broken declares a named model with a runtime type: PropType<string[]> plus a factory default: () => []. fixed declares the same thing but with the generic pinned explicitly (defineModel<string[]>(...)).
Run the type checker (or vue-tsc --noEmit locally) over this SFC.
What is expected?
Both broken and fixed should type-check cleanly — this is the recommended pattern for avoiding shared mutable state (per #14966 / #14968): a factory default combined with a runtime PropType declaration.
What is actually happening?
Only broken fails. TS infers the generic T from the default factory function's own type (() => never[]) instead of from the type: PropType<string[]> field:
error TS2769: No overload matches this call.
Overload 1 of 4, '(name: string, options: ({ required: true; } | { default: DefineModelDefault<() => never[]>; }) & Omit<PropOptions<() => never[], () => never[]>, "default"> & DefineModelOptions<...>): ModelRef<...>', gave the following error.
Type 'PropType<string[]>' is not assignable to type 'true | PropType<() => never[]> | null | undefined'.
Type 'new (...args: any[]) => string[]' is not assignable to type 'true | PropType<() => never[]> | null | undefined'.
Type 'new (...args: any[]) => string[]' is not assignable to type 'new (...args: any[]) => () => never[]'.
Type 'string[]' is not assignable to type '() => never[]'.
Type 'string[]' provides no match for the signature '(): never[]'.
Overload 2 of 4, '(name: string, options?: DefineModelRuntimeOptions<string[], string[], string[]> | undefined): ModelRef<string[] | undefined, string, string[] | undefined, string[] | undefined>', gave the following error.
Object literal may only specify known properties, and 'default' does not exist in type 'DefineModelRuntimeOptions<string[], string[], string[]>'.
his is a regression from #14968 (landed in 3.5.39), which introduced DefineModelDefault<T> = InferDefault<Data, T> for the named-model overload. When both a runtime type: PropType<T> field and a factory default are present on the same options object, TS resolves T from the default field first, breaking a pattern that worked prior to 3.5.39.
Workaround: explicitly pin the generic, as shown by fixed in the reproduction:
const fixed = defineModel<string[]>('visibility2', {
type: Array as PropType<string[]>,
default: () => [],
})
System Info
vue: 3.5.39
vue-tsc: 3.3.7
typescript: 5.7.3
Any additional comments?
Related to / regression from #14966 and #14968. Only reproduces on the named defineModel(name, options) overload when type/PropType and a factory default are combined; the unnamed defineModel<T>({ ... }) form with an explicit generic is unaffected.
The two patches
packages-private/dts-test/setupHelpers.test-d.tsindex 5ecc15443..ce23165e6 100644@@ -1,4 +1,5 @@ import {+ type PropType, type Ref, type Slots, type VNode,@@ -466,6 +467,51 @@ describe('defineModel', () => { }, }) + // runtime type + factory default (#15096)+ const typedArrayDefault = defineModel({+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(typedArrayDefault)+ const namedTypedArrayDefault = defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(namedTypedArrayDefault)+ const namedTypedObjectDefault = defineModel('obj', {+ type: Object as PropType<{ foo: string }>,+ default: () => ({ foo: 'bar' }),+ })+ expectType<Ref<{ foo: string }>>(namedTypedObjectDefault)+ const namedTypedNumberDefault = defineModel('num', {+ type: Number,+ default: () => 1,+ })+ expectType<Ref<number>>(namedTypedNumberDefault)+ const namedTypedFunctionDefault = defineModel('fn', {+ type: Function as PropType<() => void>,+ default: () => {},+ })+ expectType<Ref<() => void>>(namedTypedFunctionDefault)+ const namedInferred = defineModel('count', { default: 123 })+ expectType<Ref<number>>(namedInferred)+ const inferredFactoryDefault = defineModel({ default: () => [1] })+ expectType<Ref<number[]>>(inferredFactoryDefault)+ const namedInferredFactoryDefault = defineModel('obj', {+ default: () => ({ foo: 'bar' }),+ })+ expectType<Ref<{ foo: string }>>(namedInferredFactoryDefault)++ // @ts-expect-error runtime type / factory default mismatch+ defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [1],+ })+ // @ts-expect-error raw array defaults must use a factory+ defineModel('visibility', { type: Array as PropType<string[]>, default: [] })+ // @ts-expect-error runtime type / factory default mismatch+ defineModel({ type: Array as PropType<string[]>, default: () => 1 })+ // @ts-expect-error type / default mismatch defineModel<string>({ default: 123 }) // @ts-expect-error raw array defaults must use a factorypackages/runtime-core/src/apiSetupHelpers.tsindex 7a1d2e3a2..846eeaab5 100644@@ -273,7 +273,9 @@ export type DefineModelOptions<T = any, G = T, S = T> = { type DefineModelRuntimeOptions<T, G, S> = Omit<PropOptions<T>, 'default'> & DefineModelOptions<T, G, S> -type DefineModelDefault<T> = InferDefault<Data, T>+// `T` is inferred from `PropOptions<T>` (so `type: PropType<T>` takes priority+// over a factory `default`), this only validates the default value.+type DefineModelDefault<T> = NoInfer<InferDefault<Data, T>> /** * Vue `<script setup>` compiler macro for declaring a@@ -310,7 +312,8 @@ type DefineModelDefault<T> = InferDefault<Data, T> */ export function defineModel<T, M extends PropertyKey = string, G = T, S = T>( options: ({ default: DefineModelDefault<T> } | { required: true }) &- DefineModelRuntimeOptions<T, G, S>,+ PropOptions<T> &+ DefineModelOptions<T, G, S>, ): ModelRef<T, M, G, S> export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(@@ -320,7 +323,8 @@ export function defineModel<T, M extends PropertyKey = string, G = T, S = T>( export function defineModel<T, M extends PropertyKey = string, G = T, S = T>( name: string, options: ({ default: DefineModelDefault<T> } | { required: true }) &- DefineModelRuntimeOptions<T, G, S>,+ PropOptions<T> &+ DefineModelOptions<T, G, S>, ): ModelRef<T, M, G, S> export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(
packages-private/dts-test/setupHelpers.test-d.tsindex 5ecc15443..067b81b03 100644@@ -1,4 +1,5 @@ import {+ type PropType, type Ref, type Slots, type VNode,@@ -421,6 +422,18 @@ describe('defineModel', () => { const countDefault = defineModel<number>('count', { default: 1 }) expectType<Ref<number>>(countDefault) + const inferredNamedArrayDefault = defineModel('items', {+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(inferredNamedArrayDefault)++ const inferredArrayDefault = defineModel({+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(inferredArrayDefault)+ const arrayDefault = defineModel<number[]>({ default: () => [] }) expectType<Ref<number[]>>(arrayDefault) @@ -468,6 +481,11 @@ describe('defineModel', () => { // @ts-expect-error type / default mismatch defineModel<string>({ default: 123 })+ // @ts-expect-error runtime type / default mismatch+ defineModel('items', {+ type: Array as PropType<string[]>,+ default: () => [1],+ }) // @ts-expect-error raw array defaults must use a factory defineModel<number[]>({ default: [] }) // @ts-expect-error raw object defaults must use a factorypackages/runtime-core/src/apiSetupHelpers.tsindex 7a1d2e3a2..f71616988 100644@@ -309,8 +309,8 @@ type DefineModelDefault<T> = InferDefault<Data, T> * ``` */ export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(- options: ({ default: DefineModelDefault<T> } | { required: true }) &- DefineModelRuntimeOptions<T, G, S>,+ options: DefineModelRuntimeOptions<T, G, S> &+ ({ default: DefineModelDefault<T> } | { required: true }), ): ModelRef<T, M, G, S> export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(@@ -319,8 +319,8 @@ export function defineModel<T, M extends PropertyKey = string, G = T, S = T>( export function defineModel<T, M extends PropertyKey = string, G = T, S = T>( name: string,- options: ({ default: DefineModelDefault<T> } | { required: true }) &- DefineModelRuntimeOptions<T, G, S>,+ options: DefineModelRuntimeOptions<T, G, S> &+ ({ default: DefineModelDefault<T> } | { required: true }), ): ModelRef<T, M, G, S> export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(