Backtestsvuerun-20261007-145226#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.
Both patches target the right overloads and add regression tests for the reported pattern: a named and an unnamed `defineModel` with `type: Array as PropType<string[]>` plus `default: () => []`, along with a negative mismatch case. Those tests would fail before each change.
Patch B only swaps the order of the intersection operands, so `DefineModelRuntimeOptions` comes before the `{ default } | { required }` union. Any fix it provides relies on how TypeScript orders inference candidates. With two equal-priority candidates for `T` (`string[]` from `PropType<T>` and `() => never[]` from the naked `T` inside `InferDefault`), the common-supertype reduction keeps the leftmost candidate, so moving the `type` field first may tip inference the right way. That depends on compiler internals rather than on declared priority. It leaves the real cause in place: the default factory's own type can still be inferred as `T` at the same priority as the runtime type. It could break silently with compiler changes or with other property shapes.
Patch A goes to the root cause. It rewrites `DefineModelDefault` so that `T` is inferred from the factory only through its return type (`(props: Data) => T & {}`, a lower inference priority). The raw-value branch is wrapped in `NoInfer` so it cannot compete with `PropType<T>`, and nullish defaults are allowed explicitly. The explanatory comment fits the surrounding code. It also adds broader coverage: object, function-typed, null-default and string runtime-type cases, plus negative tests for a mismatched factory return and a raw array default. Together these protect the original #14968 guarantee (no shared mutable defaults) while fixing the regression.
The main risk in A is that `NoInfer` needs TypeScript 5.4+ for consumers, and the `Function`-typed case interacts with the factory branch in subtle ways. Even so, it is a principled fix with stronger tests, whereas B is an order-dependent workaround.
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 10s
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
51vs 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..a70e8b699 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) + // infer type from runtime type with 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 namedTypedFunctionDefault = defineModel('fn', {+ type: Function as PropType<() => void>,+ default: () => {},+ })+ expectType<Ref<() => void>>(namedTypedFunctionDefault)+ const namedTypedNullDefault = defineModel('nullable', {+ type: Object as PropType<{ foo: string }>,+ default: null,+ })+ expectType<Ref<{ foo: string }>>(namedTypedNullDefault)+ const namedTypedStringDefault = defineModel('str', {+ type: String,+ default: 'foo',+ })+ expectType<Ref<string>>(namedTypedStringDefault)+ // infer type from default const inferred = defineModel({ default: 123 }) expectType<Ref<number | undefined>>(inferred)@@ -472,6 +505,16 @@ describe('defineModel', () => { defineModel<number[]>({ default: [] }) // @ts-expect-error raw object defaults must use a factory defineModel<{ foo: string }>({ default: { foo: 'bar' } })+ defineModel('visibility', {+ type: Array as PropType<string[]>,+ // @ts-expect-error factory default must match the runtime type+ default: () => 1,+ })+ defineModel('visibility', {+ type: Array as PropType<string[]>,+ // @ts-expect-error raw array defaults must use a factory+ default: [],+ }) // @ts-expect-error unknown props option defineModel({ foo: 123 }) packages/runtime-core/src/apiSetupHelpers.tsindex 7a1d2e3a2..b5de60f47 100644@@ -273,7 +273,13 @@ 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 only inferred from `default` at the lowest (naked type variable)+// priority, so a runtime `type: PropType<T>` always wins over the default+// factory's own function type. Nullish defaults are always allowed.+type DefineModelDefault<T> =+ | ((props: Data) => T & {})+ | (T & NoInfer<T extends NativeType ? T : never>)+ | NoInfer<null | undefined> /** * Vue `<script setup>` compiler macro for declaring a
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>(