Backtestsvuerun-20261002-115633#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 B = our agent, Patch A = the merged fix.
Both patches make the same source change. In the `defineModel` overloads that take an options object (named and unnamed), they reorder the intersection so that `DefineModelRuntimeOptions<T, G, S>` comes before the `{ default: DefineModelDefault<T> } | { required: true }` union. This targets the root cause the issue describes: TS was inferring `T` from the `default` factory before it looked at the runtime `type: PropType<T>`. So on correctness the two are equal.
The difference is in tests and craft.
- **Patch A tests:** the named and unnamed `type: PropType<string[]>` + `default: () => []` cases, plus a `@ts-expect-error` case for a mismatched factory default.
- **Patch B tests:** the same three cases, plus a named object model (`PropType<{ foo: string }>` with `() => ({ foo: 'bar' })`) and a named model combining `required: true` with a factory default. These exercise more of the inference surface the regression touched.
- **Patch B comment:** it explains why the intersection order matters. That makes the fix less likely to be silently undone by a future refactor, which matters because the fix is order-dependent.
Patch A is correct but less defended against regression. With the fixes identical, Patch B's broader tests and explanatory comment make it marginally better.
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
9m 41s
Wall-clock time in the container
Full test suite
No new failures
3,626 passed · 6 skipped · 3,632 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
47vs 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..4548f8318 100644@@ -1,4 +1,5 @@ import {+ type PropType, type Ref, type Slots, type VNode,@@ -429,6 +430,38 @@ describe('defineModel', () => { }) expectType<Ref<{ foo: string }>>(objectDefault) + // runtime type + factory default should infer from `type`+ const runtimeArrayDefault = defineModel({+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(runtimeArrayDefault)++ const namedRuntimeArrayDefault = defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(namedRuntimeArrayDefault)++ const namedRuntimeObjectDefault = defineModel('obj', {+ type: Object as PropType<{ foo: string }>,+ default: () => ({ foo: 'bar' }),+ })+ expectType<Ref<{ foo: string }>>(namedRuntimeObjectDefault)++ const namedRuntimeRequiredDefault = defineModel('list', {+ type: Array as PropType<number[]>,+ required: true,+ default: () => [],+ })+ expectType<Ref<number[]>>(namedRuntimeRequiredDefault)++ // @ts-expect-error factory default must match runtime type+ defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [1],+ })+ // infer type from default const inferred = defineModel({ default: 123 }) expectType<Ref<number | undefined>>(inferred)packages/runtime-core/src/apiSetupHelpers.tsindex 7a1d2e3a2..f604c58a7 100644@@ -275,6 +275,12 @@ type DefineModelRuntimeOptions<T, G, S> = Omit<PropOptions<T>, 'default'> & type DefineModelDefault<T> = InferDefault<Data, T> +// NOTE: in the overloads below, `DefineModelRuntimeOptions` must come before+// the `default` member of the intersection so that `T` is inferred from the+// runtime `type` (e.g. `PropType<string[]>`) before the `default` factory.+// Otherwise a factory default such as `() => []` would be inferred as `T`+// itself and conflict with the declared `type`.+ /** * Vue `<script setup>` compiler macro for declaring a * two-way binding prop that can be consumed via `v-model` from the parent@@ -309,8 +315,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 +325,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>(
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>(