Backtestsvuerun-20261005-124535#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.
Patch B fixes the root cause directly. It adds a typed overload in which `T` is inferred only from `type: PropType<T>`, and the default is wrapped in `NoInfer<InferDefault<Data, T>>`. A factory like `() => []` therefore can no longer contribute an inference candidate for `T`. A separate untyped overload, tried first, rejects runtime `type` declarations. That overload infers `T` from a plain value or the factory's return value, not from the factory function itself. This addresses the mechanism the issue describes: `T` being resolved from `() => never[]`. Patch A only reorders the members of the intersection (`DefineModelRuntimeOptions & ({default}|{required})`). This relies on TypeScript's candidate ordering and common-supertype selection favouring the `type` field because it appears first. That behaviour is subtle and fragile; it may happen to work, but it masks the problem rather than stopping `default` from feeding inference. On tests, Patch B covers the exact issue scenario (named and unnamed), object/function/number factories, required and optional variants, transformers, nullable and union runtime types, and several negative cases (`raw [] default`, factory return mismatch, type/default mismatch). These give solid regression coverage. Patch A adds just the two positive cases plus one negative case. Patch B carries some risk: it adds overloads, more type complexity, and a dependency on `NoInfer` (TS 5.4+). Even so, its approach is principled and much better tested, while Patch A's fix depends on incidental ordering behaviour.
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
24m 17s
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
135vs 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..beda82621 100644@@ -1,4 +1,5 @@ import {+ type PropType, type Ref, type Slots, type VNode,@@ -466,6 +467,104 @@ describe('defineModel', () => { }, }) + // #15096 runtime type + factory default should infer from `type`+ const typedArray = defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(typedArray)+ // @ts-expect-error+ typedArray.value = [1]++ const typedArrayUnnamed = defineModel({+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(typedArrayUnnamed)+ // @ts-expect-error+ typedArrayUnnamed.value = [1]++ const typedObject = defineModel('obj', {+ type: Object as PropType<{ foo?: string }>,+ default: () => ({}),+ })+ expectType<Ref<{ foo?: string }>>(typedObject)+ // @ts-expect-error+ typedObject.value = { foo: 1 }++ const typedObjectRequired = defineModel('obj', {+ type: Object as PropType<{ foo?: string }>,+ required: true,+ })+ expectType<Ref<{ foo?: string }>>(typedObjectRequired)+ // @ts-expect-error+ typedObjectRequired.value = { foo: 1 }++ const typedObjectOptional = defineModel('obj', {+ type: Object as PropType<{ foo?: string }>,+ })+ expectType<Ref<{ foo?: string } | undefined>>(typedObjectOptional)+ // @ts-expect-error+ typedObjectOptional.value = { foo: 1 }++ const typedNumberFactory = defineModel({ type: Number, default: () => 1 })+ expectType<Ref<number>>(typedNumberFactory)+ // @ts-expect-error+ typedNumberFactory.value = 'foo'++ const typedFunction = defineModel('fn', {+ type: Function as PropType<() => void>,+ default: () => {},+ })+ expectType<Ref<() => void>>(typedFunction)++ const typedWithTransformers = defineModel('typedTransform', {+ type: String,+ default: 'foo',+ get(val) {+ return val.toLowerCase()+ },+ })+ expectType<Ref<string>>(typedWithTransformers)++ // infer from the factory's return value, not the factory itself+ const inferredFactory = defineModel({ default: () => [1] })+ expectType<number[]>(inferredFactory.value)+ // @ts-expect-error+ expectType<() => number[]>(inferredFactory.value)++ // @ts-expect-error raw array defaults must use a factory+ defineModel('visibility', { type: Array as PropType<string[]>, default: [] })+ defineModel('visibility', {+ type: Array as PropType<string[]>,+ // @ts-expect-error factory return / runtime type mismatch+ default: () => [1],+ })+ // @ts-expect-error runtime type / default mismatch+ defineModel('count', { type: String, default: 1 })+ // @ts-expect-error raw array defaults must use a factory+ defineModel<string | string[]>({ default: ['foo'] })+ defineModel<string | string[]>({ default: 'foo' })++ // runtime type unions with non-native members still accept native defaults+ const typedNullable = defineModel('nullable', {+ type: Object as PropType<{ foo?: string } | null>,+ default: null,+ })+ expectType<Ref<{ foo?: string } | null>>(typedNullable)+ const typedUnion = defineModel({+ type: [String, Array] as PropType<string | string[]>,+ default: 'foo',+ })+ expectType<Ref<string | string[]>>(typedUnion)+ // @ts-expect-error raw array defaults must use a factory+ defineModel({ type: [String, Array] as PropType<string[]>, default: ['a'] })++ // runtime type that may be `null` still infers from `type`+ const maybeType = Math.random() > 0.5 ? String : null+ const typedMaybe = defineModel({ type: maybeType, required: true })+ expectType<Ref<string>>(typedMaybe)+ // @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..9fd057980 100644@@ -30,6 +30,7 @@ import type { ComponentPropsOptions, ExtractPropTypes, PropOptions,+ PropType, } from './componentProps' import { warn } from './warning' import type { SlotsType, StrictUnwrapSlotsType } from './componentSlots'@@ -273,7 +274,25 @@ 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 a plain value or a factory's return value, never+// from the factory function itself; validation is done on the non-inferred side+type DefineModelDefault<T> = (T | ((props: Data) => T)) &+ NoInfer<InferDefault<Data, T>>++// when a runtime `type` is provided, `T` is inferred from it alone so that+// the `default` (e.g. a `() => []` factory) can't widen or override it.+// `NoInfer` wraps the whole default so `InferDefault` still distributes over+// union members (e.g. `Foo | null` accepting `default: null`)+type DefineModelTypedOptions<T, G, S> = {+ type?: PropType<T> | true | null+} & ({ default: NoInfer<InferDefault<Data, T>> } | { required: true }) &+ DefineModelRuntimeOptions<T, G, S>++// tried before `DefineModelTypedOptions`; runtime `type` declarations are+// rejected here so `T` can't be inferred from `default` instead+type DefineModelUntypedOptions<T, G, S> = (+ { default: DefineModelDefault<T> } | { required: true }+) & { type?: true | null } & DefineModelRuntimeOptions<T, G, S> /** * Vue `<script setup>` compiler macro for declaring a@@ -309,8 +328,11 @@ 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: DefineModelUntypedOptions<T, G, S>,+): ModelRef<T, M, G, S>++export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(+ options: DefineModelTypedOptions<T, G, S>, ): ModelRef<T, M, G, S> export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(@@ -319,8 +341,12 @@ 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: DefineModelUntypedOptions<T, G, S>,+): ModelRef<T, M, G, S>++export function defineModel<T, M extends PropertyKey = string, G = T, S = T>(+ name: string,+ options: DefineModelTypedOptions<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>(