Backtestsvuerun-20261001-022054#15096
defineModel: named model with PropType + factory default infers wrong generic (regression from #14968)
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
This verdict is based on reasoning about TypeScript's inference behavior; I did not run either patch's type tests. The root cause is how `T` gets inferred when the options object has both `type: PropType<string[]>` and a factory `default`. `InferDefault` contributes a candidate of `() => never[]` for `T`, and in the original intersection the `default` member comes first, so that candidate wins over `string[]` from `type`. Patch A reorders the parameter intersection so `DefineModelRuntimeOptions<T, G, S>` (which carries `type`) comes before the `default`/`required` union. The `string[]` candidate is then gathered first and acts as the common supertype of `never[]`. The function-typed candidate is unrelated, so `T` resolves to `string[]`. The change is minimal, applied to both the named and unnamed overloads, and leaves `DefineModelDefault` semantics alone, so explicit-generic cases keep their current behavior. Its tests cover the exact named-model scenario from the issue, the unnamed equivalent, and a negative case where the factory returns the wrong element type. Patch B rewrites `DefineModelDefault` from the conditional `InferDefault` to `((props) => T & {}) | (T & NativeType)`. This does not clearly fix the inference order: a naked `T` inside `T & NativeType` still receives `() => never[]` as a candidate at the same priority. Whether `T` comes out right then depends on union member and type-id ordering, which is fragile. It also changes the default-typing semantics for every use of `defineModel`, for example how deferred generic `T` and intersections with `NativeType` behave, which is broader risk than the issue calls for. Patch B's tests are more extensive (assignments, a function-typed default, a raw-array rejection), but tests count less than correctness. Patch A's targeted, low-risk fix of the inference-order root cause is the better patch.
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 24s30 turns
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
29vs 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..25386746a 100644@@ -1,4 +1,5 @@ import {+ type PropType, type Ref, type Slots, type VNode,@@ -429,6 +430,30 @@ describe('defineModel', () => { }) expectType<Ref<{ foo: string }>>(objectDefault) + // infer type from runtime type with factory default+ const namedRuntimeTypeDefault = defineModel('visibility', {+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(namedRuntimeTypeDefault)+ const runtimeTypeDefault = defineModel({+ type: Array as PropType<string[]>,+ default: () => [],+ })+ expectType<Ref<string[]>>(runtimeTypeDefault)+ namedRuntimeTypeDefault.value = ['foo']+ runtimeTypeDefault.value = ['foo']+ // @ts-expect-error+ namedRuntimeTypeDefault.value = [1]++ // function type with function default+ const fnDefault = defineModel({+ type: Function as PropType<(x: number) => void>,+ default: (_x: number) => {},+ })+ expectType<Ref<(x: number) => void>>(fnDefault)+ fnDefault.value = (_x: number) => {}+ // infer type from default const inferred = defineModel({ default: 123 }) expectType<Ref<number | undefined>>(inferred)@@ -470,6 +495,8 @@ describe('defineModel', () => { defineModel<string>({ default: 123 }) // @ts-expect-error raw array defaults must use a factory defineModel<number[]>({ default: [] })+ // @ts-expect-error raw array defaults must use a factory+ defineModel('visibility', { type: Array as PropType<string[]>, default: [] }) // @ts-expect-error raw object defaults must use a factory defineModel<{ foo: string }>({ default: { foo: 'bar' } }) // @ts-expect-error unknown props optionpackages/runtime-core/src/apiSetupHelpers.tsindex 7a1d2e3a2..bc2015fdf 100644@@ -273,7 +273,7 @@ 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>+type DefineModelDefault<T> = ((props: Data) => T & {}) | (T & NativeType) /** * 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>(
What the agent reported
I fixed it: defineModel(name, { type: Array as PropType<string[]>, default: () => [] }) now type-checks without pinning the generic. The new tests fail without the fix and pass with it.
What was wrong: #14968 changed the type allowed for default in defineModel (DefineModelDefault, packages/runtime-core/src/apiSetupHelpers.ts:276). Its "plain value" branch let TypeScript take T directly from the default value at top priority. A factory like () => [] therefore won over type: PropType<string[]>, so T became () => never[] and no overload matched. The unnamed defineModel({ type, default }) form failed the same way, not only the named one.
What I changed: one line in that file:
type DefineModelDefault<T> = ((props: Data) => T & {}) | (T & NativeType)
Writing the plain-value branch as an intersection (T & NativeType) means the default value no longer outranks type when TypeScript picks T. It still accepts only primitives and functions as plain defaults, so arrays and objects must still be factories, which was the point of #14968.
Tests (added to packages-private/dts-test/setupHelpers.test-d.ts):
- The report's exact pattern, named and unnamed, checked as
Ref<string[]>. Assigning['foo']must succeed and assigning[1]must fail, which pinsTtostring[]. - A function-typed prop with a function default. My first version of the fix broke this case, which works on 3.5.39; the reviewer caught that I had no test for it.
type: PropType<string[]>with a plaindefault: []must still be rejected.
Evidence:
- Without the fix the new tests give 6 errors (TS2769, TS2345, TS2322); with it they give none.
- The repo's own
build-dts+test-dts-onlypasses. - Root
tsc --noEmitand eslint are clean. - The full
./bt-testrun passes: 3632 tests, 0 failures.
Things to know:
- Still broken from 3.5.39:
defineModel('x', { default: () => [1] })with notypeand no explicit generic still infers() => number[]. It worked before #14968. I couldn't find a variant that fixes it without breaking the function-typed case, and it's outside this report, so it's unchanged. - Slightly looser in one place: for
T = object, a plain function is now accepted as adefault. Before #14968 any default was accepted, so this is still stricter than 3.5.38. - Existing assertion probably wrong:
defineModel({ default: 123 })is expected to beRef<number | undefined>, which may be wrong but passes becauseexpectTypeonly checks assignability. I left it alone. ./bt-testscope: it runs only vitest, which doesn't type-check the dts tests. I checked those separately withtsc.
Requirements:
- Named model with
type: PropType<T>and a factory default type-checks: done and tested. - The
fixedworkaround (explicit generic) still works: done. The existing explicit-generic tests (defineModel<number[]>({ default: () => [] })etc.) still pass. - The protections from #14968 stay intact: done. The existing
@ts-expect-errortests for plain array/object/mismatched defaults still pass, plus the new one. - The unnamed form is unaffected: it was actually affected too. It's now fixed and tested.
Changes are uncommitted and no scratch files are left in the tree.