Backtestsvuerun-20261003-231210#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.
The root cause is that `InferDefault<Data, T>` contains a conditional branch that exposes `T` as an inference site. TypeScript therefore takes a candidate for `T` from the `default` factory itself (`() => never[]`), and that candidate competes with the one from `type: PropType<string[]>`. Patch A fixes this directly. It wraps the validating `InferDefault` member in `NoInfer`, so the default can no longer drive inference. It also keeps a plain `T | factory` member so `T` is still inferable when no runtime `type` is given. Its comment explains this. The result does not depend on TypeScript's candidate-ordering heuristics. Patch B only swaps the order of the intersection members. Any fix this gives comes from the order in which inference candidates are collected and how the common supertype is chosen. That is fragile, undocumented compiler behavior, and the competing `() => never[]` candidate still exists. It may pass the narrow test, but it masks the symptom rather than removing the bad inference site, and could break under other default shapes or TypeScript versions. On tests, Patch A covers more: - named and unnamed array cases; - object and function `PropType`s with factory defaults; - a raw-array-default rejection with a runtime type; - a factory/type mismatch rejection. Patch B covers only the array case plus one mismatch. Both changes are tightly scoped. Patch A is the more robust and better-tested 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
15m 40s
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
49vs 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..30ba0644e 100644@@ -1,4 +1,5 @@ import {+ type PropType, type Ref, type Slots, type VNode,@@ -429,6 +430,31 @@ describe('defineModel', () => { }) expectType<Ref<{ foo: string }>>(objectDefault) + // infer type from runtime type when combined with a factory default+ const runtimeTypeArrayDefault = defineModel({+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(runtimeTypeArrayDefault)++ const namedRuntimeTypeArrayDefault = defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(namedRuntimeTypeArrayDefault)++ const namedRuntimeTypeObjectDefault = defineModel('obj', {+ type: Object as PropType<{ foo: string }>,+ default: () => ({ foo: 'bar' }),+ })+ expectType<Ref<{ foo: string }>>(namedRuntimeTypeObjectDefault)++ const namedRuntimeTypeFunctionDefault = defineModel('fn', {+ type: Function as PropType<() => number>,+ default: () => 1,+ })+ expectType<Ref<() => number>>(namedRuntimeTypeFunctionDefault)+ // infer type from default const inferred = defineModel({ default: 123 }) expectType<Ref<number | undefined>>(inferred)@@ -472,6 +498,13 @@ describe('defineModel', () => { defineModel<number[]>({ default: [] }) // @ts-expect-error raw object defaults must use a factory defineModel<{ foo: string }>({ default: { foo: 'bar' } })+ // @ts-expect-error raw array defaults must use a factory with runtime type+ defineModel('visibility', { type: Array as PropType<string[]>, default: [] })+ // @ts-expect-error factory default must match the runtime type+ defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [1],+ }) // @ts-expect-error unknown props option defineModel({ foo: 123 }) packages/runtime-core/src/apiSetupHelpers.tsindex 7a1d2e3a2..00cb3240d 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>+// The first member keeps `T` as a naked inference site (as `PropOptions` does)+// so it is inferred from the runtime `type` option before the `default` value,+// while the `NoInfer` member validates the default without affecting inference.+type DefineModelDefault<T> = {+ default: (T | ((props: Data) => T | null | undefined)) &+ NoInfer<InferDefault<Data, T>>+} /** * Vue `<script setup>` compiler macro for declaring a@@ -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> &+ (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> &+ (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>(