Backtestsvuerun-20261021-133314#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 make the same core change: they reorder the overload intersection so `DefineModelRuntimeOptions<T, G, S>` (which carries `type: PropType<T>`) precedes the `{ default } | { required }` union, so TS collects the `PropType<T>` candidate before the candidate derived from the factory default. The independent tests confirm both patches turn the three base failures into passes, so neither leaves the reported scenario broken.
The difference is in how deeply each addresses the root cause the issue names: 'TS infers the generic T from the default factory function's own type (() => never[])'. That candidate comes from the `T extends NativeType ? T : never` branch of `InferDefault`, which lets TS propose the whole function as T. Patch B only changes candidate ordering, so the bogus function-as-T candidate still exists and simply loses to the `PropType` candidate when one is present. Patch A also rewrites `DefineModelDefault<T>` as `(T | ((props: Data) => T)) & NoInfer<InferDefault<Data, T>>`: the naked-union form is the standard TS idiom for inferring T from a factory's return, the factory member matches first so T is never inferred as the function itself, and `NoInfer` keeps `InferDefault` purely as a validator (raw array/object defaults still must use a factory). That removes the faulty inference site rather than just outranking it, and it also covers the factory-default-without-`type` case (`defineModel('count', { default: () => [1] })`), which A explicitly asserts yields `Ref<number[]>`; B does not test or address that path.
Tests: A adds considerably broader dts coverage: named and unnamed PropType+factory, object PropType, Function-typed models (where the default is not a factory), `type: Number` with factory, `required: true` plus default, the explicit-generic workaround, plus negative cases (named raw-object default, factory/runtime mismatch for both overloads, scalar type/default mismatch). B adds the two positive cases from the issue and one mismatch negative test. Both sets would fail before and pass after for the issue scenario.
Scope and craft: A's extra type change is modest, targeted at the exact mechanism the issue identifies, and documented with a clear comment explaining both the naked-union inference and why overload member order matters. B is smaller and lower-risk but relies solely on candidate ordering. On balance A is the more complete, better-tested fix.
Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.
Solve
11m 22s
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
82vs 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..6b91271d0 100644@@ -1,4 +1,5 @@ import {+ type PropType, type Ref, type Slots, type VNode,@@ -435,6 +436,60 @@ describe('defineModel', () => { const inferredRequired = defineModel({ default: 123, required: true }) expectType<Ref<number>>(inferredRequired) + // infer type from runtime type + factory default (#15096)+ 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)+ // `Function` typed defaults are used as-is (not called as a factory)+ const runtimeFunctionDefault = defineModel({+ type: Function as PropType<() => void>,+ default: () => {},+ })+ expectType<Ref<() => void>>(runtimeFunctionDefault)+ const namedRuntimeFunctionDefault = defineModel('fn', {+ type: Function as PropType<(x: number) => string>,+ default: (x: number) => String(x),+ })+ expectType<Ref<(x: number) => string>>(namedRuntimeFunctionDefault)+ const namedFunctionDefault = defineModel('fn2', {+ type: Function,+ default: () => {},+ })+ expectType<Ref<Function>>(namedFunctionDefault)+ const namedRuntimeNumberDefault = defineModel('num', {+ type: Number,+ default: () => 1,+ })+ expectType<Ref<number>>(namedRuntimeNumberDefault)+ const namedRuntimeRequiredDefault = defineModel('list', {+ type: Array as PropType<string[]>,+ required: true,+ default: () => [],+ })+ expectType<Ref<string[]>>(namedRuntimeRequiredDefault)+ const namedRuntimeExplicitDefault = defineModel<string[]>('list2', {+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(namedRuntimeExplicitDefault)+ // infer type from factory default+ const inferredFactory = defineModel({ default: () => [1] })+ expectType<Ref<number[]>>(inferredFactory)+ const namedInferredFactory = defineModel('count', { default: () => [1] })+ expectType<Ref<number[]>>(namedInferredFactory)+ // modifiers const [_, modifiers] = defineModel<string>() expectType<true | undefined>(modifiers.foo)@@ -472,6 +527,14 @@ 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 object defaults must use a factory+ defineModel<{ foo: string }>('foo', { default: { foo: 'bar' } })+ // @ts-expect-error factory default / runtime type mismatch+ defineModel({ type: Array as PropType<string[]>, default: () => [1] })+ // @ts-expect-error factory default / runtime type mismatch+ defineModel('foo', { type: Array as PropType<string[]>, default: () => [1] })+ // @ts-expect-error default / runtime type mismatch+ defineModel('foo', { type: String, default: 1 }) // @ts-expect-error unknown props option defineModel({ foo: 123 }) packages/runtime-core/src/apiSetupHelpers.tsindex 7a1d2e3a2..1e5cca48b 100644@@ -273,7 +273,16 @@ 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 | factory` is the inference site: since `T` is naked in the union, TS+// matches a factory function against the factory member first and never+// infers `T` as the function itself (which would conflict with a runtime+// `type: PropType<T>`). The `NoInfer` part only validates the default+// (e.g. object / array defaults must be wrapped in a factory).+// Note: the `defineModel` overloads list the runtime options before the+// default so that a `type: PropType<T>` candidate takes precedence over the+// default's (e.g. a raw function default for a `Function` typed model).+type DefineModelDefault<T> = (T | ((props: Data) => T)) &+ NoInfer<InferDefault<Data, T>> /** * Vue `<script setup>` compiler macro for declaring a@@ -309,8 +318,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 +328,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>(