Backtestsvuerun-20261013-093701#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.
Patch A fixes the root cause directly. `PropMethod<T>` was a conditional type on `T`. When `T` is a generic type parameter, `PropType<T>` becomes deferred, which in turn defers `RequiredKeys`/`OptionalKeys` and drops non-required props. A makes `PropMethod` a plain function-like constructor type, so the conditional disappears and the key computations resolve again. It adds one small, targeted adjustment to `InferPropType`: fall back to `V` when `D` is unknown, so a deferred generic stays assignable to `T`. Removing the conditional looks low-risk. Mismatched constructors still fail because the `(): T` call signature must match, and the existing inference branches for Object/Boolean/Date/array come before the `Prop<infer V, infer D>` branch. A's dts tests cover the shorthand `Object as PropType<T>`, the required case, a default-null case, a plain String prop, the key set, and assignability to `{ bar: T }`. They do not exercise the exact `{ type: Object as PropType<T> }` without `required`, but the root-cause change covers that path as well.
Patch B leaves the deferred conditional in place and works around it everywhere. It rewrites `RequiredKeys`, `OptionalKeys`, `DefaultKeys`, `InferPropType`, `ExtractPropTypes`, `PublicRequiredKeys` and `PublicOptionalKeys` using `{ v: X } extends { v: Y }` wrappers. It also adds an `IsSameType` identity trick and a complex `IsUndefinedDefault` with factory-return inference, and it changes `InferPropType`'s default-fallback logic. This large redesign of core prop typing carries significant regression risk for existing non-generic users, through subtle changes in distributivity and inference and in how the `OptionalKeys` per-key `Pick` behaves. It also obscures the actual cause. B's tests are more thorough: they mirror the issue's a/b/c cases and also cover public props and defaults. But that thoroughness does not outweigh how much more invasive and fragile B's implementation is compared with A's minimal 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
19m 9s
Wall-clock time in the container
Full test suite
No new failures
1,824 passed · 1 skipped · 1,825 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
185vs 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..804607ac8 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,58 @@ expectType<number | undefined>(publicProps.foo) expectType<string>(publicProps.bar) expectType<boolean | undefined>(publicProps.baz) expectType<unknown[] | undefined>(publicProps.qux)++describe('generic prop types', <T>() => {+ // #9546+ 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,+ },+ e: Boolean,+ } as const++ // internal facing props+ const props = {} as Prettify<ExtractPropTypes<typeof genericPropsOptions>>++ expectType<T | undefined>(props.a)+ // @ts-expect-error optional prop may be undefined+ expectType<T>(props.a)+ expectType<T | undefined>(props.b)+ // @ts-expect-error optional prop may be undefined+ expectType<T>(props.b)+ expectType<T>(props.c)+ expectType<T>(props.d)+ expectType<boolean>(props.e)++ // external facing props+ const publicProps = {} as Prettify<+ 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 may be undefined+ expectType<T>(publicProps.d)+ expectType<boolean | undefined>(publicProps.e)++ // props with defaults+ const defaults = {} as ExtractDefaultPropTypes<typeof genericPropsOptions>++ expectType<T>(defaults.d)+ expectType<boolean>(defaults.e)+ // @ts-expect-error no default+ defaults.a+ // @ts-expect-error no default+ defaults.c+})packages-private/dts-test/setupHelpers.test-d.tsindex 067b81b03..1919013b4 100644@@ -268,6 +268,39 @@ describe('withDefaults w/ defineProp discriminate union type', () => { } }) +describe('defineProps w/ generic runtime declaration', <T>() => {+ // #9546+ 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: Boolean,+ f: String,+ })++ expectType<T | undefined>(props.a)+ // @ts-expect-error optional prop may be undefined+ expectType<T>(props.a)+ expectType<T | undefined>(props.b)+ // @ts-expect-error optional prop may be undefined+ expectType<T>(props.b)+ expectType<T>(props.c)+ expectType<T>(props.d)+ expectType<boolean>(props.e)+ expectType<string | undefined>(props.f)+ // @ts-expect-error+ props.g+})+ describe('defineProps w/ runtime declaration', () => { // runtime declaration const props = defineProps({packages/runtime-core/src/componentProps.tsindex 94782e17c..3fd56a728 100644@@ -78,55 +78,98 @@ type PropMethod<T, TConstructor = any> = [T] extends [ ? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor : never +// Conditional types whose checked type is (or contains at the top level) a+// generic `PropType<T>` are deferred by TypeScript, because `PropMethod<T>` is+// itself an unresolvable conditional type while `T` is a type parameter+// (e.g. `<script setup generic="T">`). Wrapping the checked type in an object+// (`{ v: X } extends { v: Y }`) has the same assignability semantics as+// `[X] extends [Y]`, but lets TypeScript resolve the check eagerly.+type Extends<T, U> = { v: T } extends { v: U } ? true : false++// exact type identity, which resolves for identical generic types+type IsSameType<A, B> =+ (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2+ ? true+ : false++type IsUndefinedDefault<T> =+ Extends<T, { default: any }> extends false+ ? false+ : Extends<T, { default: undefined }> extends true+ ? true+ : { v: T } extends { v: { type: infer P; default: infer F } }+ ? // a default factory returning exactly the declared `PropType<D>`+ // (e.g. `() => T` for a generic `T`) is a real default+ F extends () => infer D+ ? IsSameType<P, PropType<D>> extends true+ ? false+ : Extends<T, { default: () => undefined }>+ : Extends<T, { default: () => undefined }>+ : Extends<T, { default: () => undefined }>+ type RequiredKeys<T> = {- [K in keyof T]: T[K] extends+ [K in keyof T]: Extends<+ T[K], | { required: true } | { default: any } // don't mark Boolean props as undefined | BooleanConstructor | { type: BooleanConstructor }- ? T[K] extends { default: undefined | (() => undefined) }+ > extends true+ ? IsUndefinedDefault<T[K]> extends true ? never : K : never }[keyof T] -type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>+// computed per key instead of `Exclude<keyof T, RequiredKeys<T>>`, so that a+// key whose requiredness can't be resolved doesn't hide every optional key+type OptionalKeys<T> = {+ [K in keyof T]: K extends RequiredKeys<Pick<T, K>> ? never : K+}[keyof T] type DefaultKeys<T> = {- [K in keyof T]: T[K] extends+ [K in keyof T]: Extends<+ T[K], | { default: any } // Boolean implicitly defaults to false | BooleanConstructor | { type: BooleanConstructor }- ? T[K] extends { type: BooleanConstructor; required: true } // not default if Boolean is marked as required+ > extends true+ ? Extends<T[K], { type: BooleanConstructor; required: true }> extends true // not default if Boolean is marked as required ? never : K : never }[keyof T] -type InferPropType<T, NullAsAny = true> = [T] extends [null]+type InferPropType<T, NullAsAny = true> = { v: T } extends { v: null } ? NullAsAny extends true ? any : null- : [T] extends [{ type: null | true }]+ : { v: T } extends { v: { type: null | true } } ? any // As TS issue https://github.com/Microsoft/TypeScript/issues/14829 // somehow `ObjectConstructor` when inferred from { (): T } becomes `any` // `BooleanConstructor` when inferred from PropConstructor(with PropMethod) becomes `Boolean`- : [T] extends [ObjectConstructor | { type: ObjectConstructor }]+ : { v: T } extends { v: ObjectConstructor | { type: ObjectConstructor } } ? Record<string, any>- : [T] extends [BooleanConstructor | { type: BooleanConstructor }]+ : { v: T } extends {+ v: BooleanConstructor | { type: BooleanConstructor }+ } ? boolean- : [T] extends [DateConstructor | { type: DateConstructor }]+ : { v: T } extends { v: DateConstructor | { type: DateConstructor } } ? Date- : [T] extends [(infer U)[] | { type: (infer U)[] }]+ : { v: T } extends { v: (infer U)[] | { type: (infer U)[] } } ? U extends DateConstructor ? 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- : V+ : { v: T } extends { v: Prop<infer V, infer D> }+ ? IfAny<D, false, unknown extends D ? true : false> extends true+ ? V // no default type to fall back to+ : IsSameType<V, D> extends true+ ? V+ : unknown extends V+ ? keyof V extends never+ ? IfAny<V, V, D>+ : V+ : V : T /**@@ -141,7 +184,10 @@ type InferPropType<T, NullAsAny = true> = [T] extends [null] export type ExtractPropTypes<O> = { // use `keyof Pick<O, RequiredKeys<O>>` instead of `RequiredKeys<O>` to // support IDE features- [K in keyof Pick<O, RequiredKeys<O>>]: O[K] extends { default: any }+ [K in keyof Pick<O, RequiredKeys<O>>]: Extends<+ O[K],+ { default: any }+ > extends true ? Exclude<InferPropType<O[K]>, undefined> : InferPropType<O[K]> } & {@@ -151,10 +197,12 @@ export type ExtractPropTypes<O> = { } type PublicRequiredKeys<T> = {- [K in keyof T]: T[K] extends { required: true } ? K : never+ [K in keyof T]: Extends<T[K], { required: true }> extends true ? K : never }[keyof T] -type PublicOptionalKeys<T> = Exclude<keyof T, PublicRequiredKeys<T>>+type PublicOptionalKeys<T> = {+ [K in keyof T]: Extends<T[K], { required: true }> extends true ? never : K+}[keyof T] /** * Extract prop types from a runtime props options object.
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