Backtestsvuerun-20261005-155915#9546
defineProps options object loses properties when using generics
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches find the same root cause. `PropMethod` was a conditional type on `T`, so `PropType<T>` stayed deferred whenever `T` was a type parameter. That in turn deferred `RequiredKeys`/`OptionalKeys`, which dropped the optional props `a` and `b`. Both make `PropMethod` non-conditional, so both fix the exact scenario in the issue.
Patch A goes further, and the extra work hardens the same generic-PropType path rather than adding unrelated churn:
- **Generic defaults.** It replaces the assignability check `T[K] extends { default: undefined | (() => undefined) }` with an identity-based `IsUndefinedDefault`. The old check would itself defer for a default like `() => T`, and A's tests cover that case (`d`).
- **Early resolution in `InferPropType`.** It returns `V` directly when there is no default, or when the default has the same type, using an `IsSameType` identity trick. Generic props then resolve to a concrete `T | undefined` instead of a deferred conditional.
- **Safer constructor default.** It sets `TConstructor` to `Function` instead of `any`, a small safety step against confusion with `BooleanConstructor` now that `PropMethod` is always part of the union.
Patch B's `InferPropType` tweak (`unknown extends D ? V : D`) does not resolve the type. It leaves the type deferred and only makes every branch collapse to `V`, so it relies on TypeScript's assignability rules for deferred conditional types. That works for `expectType`, but it is a weaker fix. B also does not address `RequiredKeys` deferral when a default factory returns the generic type.
On tests, A's are broader. They cover all three cases from the issue, plus a generic default factory, union and function `PropType`s, mixed non-generic props, an unknown-prop error, and `ExtractPropTypes`, `ExtractPublicPropTypes` and `ExtractDefaultPropTypes` directly, with negative `@ts-expect-error` checks. B's tests cover the issue's cases plus `default: null` and an assignability check, but nothing for generic defaults.
A is more complex, but its comments explain why each piece exists. Overall it 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
22m 37s
Wall-clock time in the container
Full test suite
No new failures
3,742 passed · 6 skipped · 3,749 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
147vs 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/extractProps.test-d.tsindex 3a5acc66e..2e453ee0c 100644@@ -1,5 +1,10 @@-import type { ExtractPropTypes, ExtractPublicPropTypes } from 'vue'-import { type Prettify, expectType } from './utils'+import type {+ ExtractDefaultPropTypes,+ ExtractPropTypes,+ ExtractPublicPropTypes,+ PropType,+} from 'vue'+import { type Prettify, describe, expectType } from './utils' const propsOptions = { foo: {@@ -28,3 +33,47 @@ expectType<number | undefined>(publicProps.foo) expectType<string>(publicProps.bar) expectType<boolean | undefined>(publicProps.baz) expectType<unknown[] | undefined>(publicProps.qux)++describe('extract props w/ generic PropType', <T>() => {+ const genericPropsOptions = {+ a: Object as PropType<T>,+ b: {+ type: Object as PropType<T>,+ },+ c: {+ type: Object as PropType<T>,+ required: true,+ },+ d: {+ type: Object as PropType<T>,+ default: () => ({}) as T,+ },+ str: String,+ } as const++ // internal facing props+ const props = {} as ExtractPropTypes<typeof genericPropsOptions>+ expectType<T | undefined>(props.a)+ // @ts-expect-error optional prop+ expectType<T>(props.a)+ expectType<T | undefined>(props.b)+ expectType<T>(props.c)+ expectType<Exclude<T, undefined>>(props.d)+ expectType<string | undefined>(props.str)++ // external facing props+ const publicProps = {} as ExtractPublicPropTypes<typeof genericPropsOptions>+ expectType<T | undefined>(publicProps.a)+ expectType<T | undefined>(publicProps.b)+ expectType<T>(publicProps.c)+ expectType<T | undefined>(publicProps.d)+ // @ts-expect-error optional prop+ expectType<T>(publicProps.d)+ expectType<string | undefined>(publicProps.str)++ // props with defaults+ const defaults = {} as ExtractDefaultPropTypes<typeof genericPropsOptions>+ expectType<T>(defaults.d)+ // @ts-expect-error no default+ defaults.a+})packages-private/dts-test/setupHelpers.test-d.tsindex 067b81b03..caf2984ed 100644@@ -299,6 +299,43 @@ describe('defineProps w/ runtime declaration', () => { props2.baz }) +describe('defineProps w/ runtime declaration + generic PropType', <T>() => {+ const props = defineProps({+ a: Object as PropType<T>,+ b: {+ type: Object as PropType<T>,+ },+ c: {+ type: Object as PropType<T>,+ required: true,+ },+ d: {+ type: Object as PropType<T>,+ default: () => ({}) as T,+ },+ e: [Object, Array] as PropType<T>,+ f: Function as PropType<(arg: T) => void>,+ // non-generic props declared alongside generic ones+ str: String,+ bool: Boolean,+ })++ expectType<T | undefined>(props.a)+ // @ts-expect-error optional prop+ expectType<T>(props.a)+ expectType<T | undefined>(props.b)+ // @ts-expect-error optional prop+ expectType<T>(props.b)+ expectType<T>(props.c)+ expectType<Exclude<T, undefined>>(props.d)+ expectType<T | undefined>(props.e)+ expectType<((arg: T) => void) | undefined>(props.f)+ expectType<string | undefined>(props.str)+ expectType<boolean>(props.bool)+ // @ts-expect-error unknown prop+ props.foo+})+ describe('defineEmits w/ type declaration', () => { const emit = defineEmits<(e: 'change') => void>() emit('change')packages/runtime-core/src/componentProps.tsindex 94782e17c..5a2d831b0 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<(...args) => R>`+// is accepted. It must not be a conditional type on `T`: otherwise it stays+// deferred for generic `T` and breaks the prop key classification below+// (see RequiredKeys). `TConstructor` defaults to `Function` rather than `any`+// so that it can never be mistaken for `BooleanConstructor`.+type PropMethod<T, TConstructor = Function> = {+ new (): TConstructor+ (): T+ readonly prototype: TConstructor+} type RequiredKeys<T> = { [K in keyof T]: T[K] extends@@ -85,12 +90,27 @@ type RequiredKeys<T> = { // don't mark Boolean props as undefined | BooleanConstructor | { type: BooleanConstructor }- ? T[K] extends { default: undefined | (() => undefined) }- ? never+ ? T[K] extends { default: infer D }+ ? IsUndefinedDefault<D> extends true+ ? never+ : K : K : never }[keyof T] +// whether a prop default is `undefined` or a factory returning `undefined`.+// Uses strict type identity rather than assignability so that it resolves+// when the default is typed with a generic type parameter, e.g. `() => T`.+type IsUndefinedDefault<D> = IfAny<+ D,+ true,+ D extends (...args: infer A) => any+ ? IsSameType<D, (...args: A) => undefined> extends true+ ? true+ : IsSameType<D, (...args: A) => never>+ : IsSameType<D, undefined>+>+ type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>> type DefaultKeys<T> = {@@ -122,13 +142,28 @@ type InferPropType<T, NullAsAny = true> = [T] extends [null] ? Date | InferPropType<U, false> : InferPropType<U, false> : [T] extends [Prop<infer V, infer D>]- ? unknown extends V- ? keyof V extends never- ? IfAny<V, V, D>- : V+ ? // without a default (or with a default of the same type) the+ // result can only be `V`: resolve it early so that it isn't+ // deferred when `V` is a generic type parameter+ [T] extends [{ default: any }]+ ? IsSameType<V, D> extends true+ ? V+ : InferPropTypeFromDefault<V, D> : V : T +type InferPropTypeFromDefault<V, D> = unknown extends V+ ? keyof V extends never+ ? IfAny<V, V, D>+ : V+ : V++// strict type identity check that also resolves for identical generic types+type IsSameType<A, B> =+ (<X>() => X extends A ? 1 : 2) extends <X>() => X extends B ? 1 : 2+ ? true+ : false+ /** * Extract prop types from a runtime props options object. * The extracted types are **internal** - i.e. the resolved props received by
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