Backtestsvuerun-20261001-022054#9546
defineProps options object loses properties when using generics
The merged fix was judged better than our patch.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the root cause the same way. They replace the conditional `PropMethod<T>` with an unconditional function-like constructor type. The old conditional kept `PropType<T>` deferred when `T` was a generic parameter, so `RequiredKeys`/`OptionalKeys` could not resolve and non-required props were dropped from `ExtractPropTypes`. Correctness on the reported symptom (missing keys) is therefore equivalent.
Patch A goes one step further in `InferPropType`. In the `unknown extends V` / `keyof V extends never` branch, it falls back to `V` when `D` is `unknown`, instead of returning `D`. For a generic `T` this makes the inferred prop type `T` rather than an opaque `D`. The issue asks for "the props to be declared correctly", not just present as keys, so this matters. The change only alters results when `V` is an unresolved generic (for concrete `unknown`/`any`, `V` and `D` coincide or `IfAny` short-circuits), so it adds little risk.
Tests are the clearer differentiator. Patch B's test only asserts that `keyof typeof props` equals `'a' | 'b' | 'c'`. That covers the regression but not whether the prop types are right. Patch A asserts:
- the key set;
- the value types: `T | null` with a null default, `T` when required, `T | undefined` for shorthand, `string | undefined` for a non-generic prop;
- negative `@ts-expect-error` cases;
- assignability of `props` to `{ bar: T }`.
These would catch regressions that Patch B's test would miss.
Scope: Patch A's extra `InferPropType` tweak is small and well commented. Craft is similar; both explain why `PropMethod` must not be conditional. Overall Patch A is the more complete and better-verified 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
6m 36s23 turns
Wall-clock time in the container
Full test suite
No new failures
3,743 passed · 6 skipped · 3,749 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
29vs 54 merged
Changed lines, ours vs merged
The issue
View on GitHubVue version
3.3.7 & 3.4.alpha
Link to minimal reproduction
Steps to reproduce
<img width="443" alt="image" src="https://github.com/vuejs/core/assets/4620458/71dad487-a49c-4c18-8fa1-05851ec62fd2">
<script setup lang="ts" generic="T">
import { PropType } from 'vue'
const props = defineProps({
a: Object as PropType<T>,
b: {
type: Object as PropType<T>
},
c: {
type: Object as PropType<T>,
required: true,
}
})
props.a
props.b
props.c
</script>
What is expected?
The props to be declared correctly
What is actually happening?
Properties are been excluded from props, unless they are required 🤔
System Info
_No response_
Any additional comments?
_No response_
The two patches
packages-private/dts-test/setupHelpers.test-d.tsindex 067b81b03..73e76fb35 100644@@ -54,6 +54,22 @@ describe('defineProps w/ generics', () => { test() }) +describe('defineProps w/ runtime props + generics', () => {+ function test<T>() {+ const props = defineProps({+ a: Object as PropType<T>,+ b: { type: Object as PropType<T> },+ c: { type: Object as PropType<T>, required: true },+ })+ props.a+ props.b+ props.c+ expectType<'a' | 'b' | 'c'>({} as keyof typeof props)+ expectType<keyof typeof props>({} as 'a' | 'b' | 'c')+ }+ test()+})+ describe('defineProps w/ type declaration + withDefaults', <T extends string>() => { const res = withDefaults(packages/runtime-core/src/componentProps.tsindex 94782e17c..09a868990 100644@@ -72,11 +72,14 @@ export type PropType<T> = PropConstructor<T> | (PropConstructor<T> | null)[] type PropConstructor<T = any> = { new (...args: any[]): T & {} } | { (): T } | PropMethod<T> -type PropMethod<T, TConstructor = any> = [T] extends [- ((...args: any) => any) | undefined,-] // if is function with args, allowing non-required functions- ? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor- : never+// Function-like constructor, allowing `Function as PropType<(a: string) => void>`.+// Not a conditional type on T, otherwise PropType<T> stays deferred for generic+// T and breaks RequiredKeys / OptionalKeys+type PropMethod<T, TConstructor = any> = {+ new (): TConstructor+ (): T+ readonly prototype: TConstructor+} type RequiredKeys<T> = { [K in keyof T]: T[K] extends
packages-private/dts-test/setupHelpers.test-d.tsindex 067b81b03..6402aea3e 100644@@ -299,6 +299,40 @@ describe('defineProps w/ runtime declaration', () => { props2.baz }) +describe('defineProps w/ runtime declaration + generic PropType', <T extends+ Record<string, any>>() => {+ // #9546+ const props = defineProps({+ foo: {+ type: Object as PropType<T>,+ required: false,+ default: null,+ },+ bar: {+ type: Object as PropType<T>,+ required: true,+ },+ baz: Object as PropType<T>,+ qux: String,+ })+ expectType<'foo' | 'bar' | 'baz' | 'qux'>(null! as keyof typeof props)+ expectType<T | null>(props.foo)+ expectType<T>(props.bar)+ expectType<T | undefined>(props.baz)+ expectType<string | undefined>(props.qux)++ // @ts-expect-error should not be a string+ expectType<string>(props.foo)+ // @ts-expect-error should not be a string+ expectType<string>(props.bar)+ // @ts-expect-error should not be a string+ expectType<string>(props.baz)++ // #9277: props should be usable where `T` is expected+ const use = (p: { bar: T }) => p+ use(props)+})+ describe('defineEmits w/ type declaration', () => { const emit = defineEmits<(e: 'change') => void>() emit('change')packages/runtime-core/src/componentProps.tsindex 94782e17c..eccb28a6c 100644@@ -72,11 +72,16 @@ export type PropType<T> = PropConstructor<T> | (PropConstructor<T> | null)[] type PropConstructor<T = any> = { new (...args: any[]): T & {} } | { (): T } | PropMethod<T> -type PropMethod<T, TConstructor = any> = [T] extends [- ((...args: any) => any) | undefined,-] // if is function with args, allowing non-required functions- ? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor- : never+// Function-like constructor so that `Function as PropType<() => void>`+// type-checks. Intentionally not conditional on `T`: a conditional type here+// makes `PropType<T>` a deferred (generic) type whenever `T` is a type+// parameter, which in turn defers `RequiredKeys` / `OptionalKeys` and drops+// every non-required prop from `ExtractPropTypes` (#9546).+type PropMethod<T, TConstructor = any> = {+ new (): TConstructor+ (): T+ readonly prototype: TConstructor+} type RequiredKeys<T> = { [K in keyof T]: T[K] extends@@ -124,7 +129,10 @@ type InferPropType<T, NullAsAny = true> = [T] extends [null] : [T] extends [Prop<infer V, infer D>] ? unknown extends V ? keyof V extends never- ? IfAny<V, V, D>+ ? // `D` is only meaningful when a default is present; falling+ // back to `V` keeps the deferred type assignable to `V`+ // when `V` is a type parameter (#9546)+ IfAny<V, V, unknown extends D ? V : D> : V : V : T
What the agent reported
I fixed the bug: inside a generic <script setup generic="T">, the optional props a and b from the report now show up on props. One thing is still open: those props' types stay unresolved for an unknown T, so const x: T | undefined = props.a still gives a type error (details below).
What was wrong
In packages/runtime-core/src/componentProps.ts, every PropType<T> includes a member called PropMethod<T>. It's what lets Function as PropType<(a: string) => void> compile. It was written as a conditional type that depends on T. When T is a generic, TypeScript can't evaluate that conditional yet, so it puts it off. The step that sorts props into required and optional was then put off too for a: Object as PropType<T>, and that took the whole required/optional split with it. As a result, keyof on the props lost every non-required prop: a and b disappeared. Only c stayed, because required: true decides it directly.
What I changed
- The fix (
componentProps.ts:75-82):PropMethodno longer depends onT. It is now always{ new (): TConstructor; (): T; readonly prototype: TConstructor }. Before, this was what you got for function types andneverfor everything else. The new member is a narrower version of the{ (): T }member that is already in the same union, so what you can assign toPropType<T>doesn't change. I added a 3-line comment explaining why it must not depend onT. - The test (
packages-private/dts-test/setupHelpers.test-d.ts): a newdescribe('defineProps w/ runtime props + generics')block with the exacta/b/ccase from the report. It readsprops.a,props.bandprops.c, and checks that the prop keys are exactly'a' | 'b' | 'c'.
Test evidence
./bt-test only runs the vitest unit tests, so it can't check types. I checked the type tests by running tsc on packages-private/dts-test against the source. The usual test-dts script needs built .d.ts files, which this checkout doesn't have. That tsc setup lives under /tmp, outside the repo.
- New test before the fix: fails with the reported errors: TS2339 "Property 'a' does not exist" and the same for
'b', plus the key check. - New test after the fix: passes (exit 0).
- All type test files: no new errors. The only two errors are the same ones as before the change, about
Symbol.matchAllinref.test-d.ts. They come from mytscsetup, not from this change. ./bt-testoncomponentProps.spec.tsandapiSetupHelpers.spec.ts: 48/48 passed.- Is
PropMethodstill needed? The reviewer thought it might now be redundant. I removed it as an experiment: the existingFunction as PropType<…>casts incomponent.test-d.ts(lines 102–133) then fail with TS2352. So it is still needed, and the comment describing what it does is accurate.
Requirements
props.a(bareObject as PropType<T>) is declared: done; the new test fails before the fix and passes after.props.b({ type: PropType<T> }) is declared: done; same test.props.c(required) stays declared, with no extra keys: done; the key check passes before and after.- "The props to be declared correctly" (their types): partly done. The props now exist, but their types are still unresolved for an unknown
T. That comes from a separate step that works out each prop's value type, and it happens with or withoutrequired: true. In the original report's code, assigningprops.ctoTalso failed. Fixing it would mean changing that value-type inference, which goes beyond this minimal fix. For the same reason the test doesn't check the props' value types (expectType<T | undefined>(props.a)): that would fail even with the fix.