Backtestsvuerun-20261009-161443#9546
defineProps options object loses properties when using generics
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the same root cause the same way. They replace the conditional `PropMethod<T>` with an unconditional function-like constructor type. That stops `PropType<T>` from becoming a deferred conditional when `T` is a type parameter, and that deferral is what caused `RequiredKeys`/`OptionalKeys` to drop non-required props. The difference is in `InferPropType`.
Patch A keeps the `unknown extends V ? keyof V extends never ? ...` chain. It only swaps the inner `D` for `unknown extends D ? V : D`. When `V` is a generic `T`, `unknown extends V` stays deferred, so the result is still an unresolved conditional type. Patch A relies on TypeScript treating that deferred conditional as assignable to `T` when both branches are. That probably passes `expectType`, but users still get an opaque conditional type rather than `T`, and it is a less direct fix.
Patch B adds an earlier check: `[T] extends [PropType<any> | { type: PropType<any> }]`. For an explicit runtime type it returns `V` directly. Because the prop-options object type is not itself a generic object type, this resolves eagerly to a clean `T`. That is the more robust result.
There is a modest risk that B's short-circuit changes behaviour for explicit-type props where `V` is `unknown` and a default supplies `D`. That is an edge case, and the existing suites would likely catch it.
On tests, B is clearly stronger. It covers exactly the issue's three shapes (bare `PropType<T>`, `{type}`, `{type, required}`) plus defaults, arrays, union constructors, function props with generic args, Boolean/String alongside generics, readonly-ness and unknown-key errors. It covers both `defineProps` and `defineComponent` (`setup` and `this`), and it also checks `ExtractPropTypes`, `ExtractPublicPropTypes` and `ExtractDefaultPropTypes`. A's test is narrower: it constrains `T extends Record<string, any>` and checks only a few cases plus assignability.
Scope is comparable. Overall B gives a cleaner inferred type and much broader regression coverage.
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
10m 45s
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
151vs 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/defineComponent.test-d.tsxindex 2c31c5d29..966258b64 100644@@ -509,6 +509,38 @@ describe('type inference w/ options API', () => { }) }) +// #9546+describe('type inference w/ generic runtime props', () => {+ function test<T>() {+ defineComponent({+ props: {+ a: Object as PropType<T>,+ b: {+ type: Object as PropType<T>,+ },+ c: {+ type: Object as PropType<T>,+ required: true,+ },+ d: Function as PropType<(arg: T) => void>,+ },+ setup(props) {+ expectType<T | undefined>(props.a)+ expectType<T | undefined>(props.b)+ expectType<T>(props.c)+ expectType<((arg: T) => void) | undefined>(props.d)+ },+ mounted() {+ expectType<T | undefined>(this.a)+ expectType<T | undefined>(this.b)+ expectType<T>(this.c)+ expectType<((arg: T) => void) | undefined>(this.d)+ },+ })+ }+ test()+})+ // #4051 describe('type inference w/ empty prop object', () => { const MyComponent = defineComponent({packages-private/dts-test/extractProps.test-d.tsindex 3a5acc66e..c6c18533a 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,46 @@ expectType<number | undefined>(publicProps.foo) expectType<string>(publicProps.bar) expectType<boolean | undefined>(publicProps.baz) expectType<unknown[] | undefined>(publicProps.qux)++// #9546+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: () => ({}),+ },+ bool: Boolean,+ } as const++ const props = {} as Prettify<ExtractPropTypes<typeof genericPropsOptions>>+ expectType<T | undefined>(props.a)+ expectType<T | undefined>(props.b)+ expectType<T>(props.c)+ expectType<T>(props.d)+ expectType<boolean>(props.bool)++ 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)+ expectType<boolean | undefined>(publicProps.bool)++ const defaults = {} as Prettify<+ ExtractDefaultPropTypes<typeof genericPropsOptions>+ >+ expectType<T>(defaults.d)+ expectType<boolean>(defaults.bool)+ // @ts-expect-error a has no default+ defaults.a+})packages-private/dts-test/setupHelpers.test-d.tsindex 067b81b03..1473a68ed 100644@@ -299,6 +299,50 @@ describe('defineProps w/ runtime declaration', () => { props2.baz }) +// #9546+describe('defineProps w/ generic runtime declaration', <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: () => ({}),+ },+ e: {+ type: Array as PropType<T[]>,+ },+ f: [String, Object] as PropType<T>,+ g: Function as PropType<(arg: T) => void>,+ h: {+ type: Function as PropType<(arg: T) => T>,+ required: true,+ },+ bool: Boolean,+ str: String,+ })++ expectType<T | undefined>(props.a)+ expectType<T | undefined>(props.b)+ expectType<T>(props.c)+ expectType<T>(props.d)+ expectType<T[] | undefined>(props.e)+ expectType<T | undefined>(props.f)+ expectType<((arg: T) => void) | undefined>(props.g)+ expectType<(arg: T) => T>(props.h)+ expectType<boolean>(props.bool)+ expectType<string | undefined>(props.str)+ // @ts-expect-error+ props.unknown+ // @ts-expect-error should be readonly+ props.a = undefined+})+ describe('defineEmits w/ type declaration', () => { const emit = defineEmits<(e: 'change') => void>() emit('change')packages/runtime-core/src/componentProps.tsindex 94782e17c..794175946 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, so that `Function as PropType<(...args) => any>`+// works. It must not be a conditional type on `T`, otherwise it is deferred+// when `T` is a generic type parameter and the props are dropped (#9546)+type PropMethod<T, TConstructor = any> = {+ new (): TConstructor+ (): T+ readonly prototype: TConstructor+} type RequiredKeys<T> = { [K in keyof T]: T[K] extends@@ -122,11 +125,13 @@ 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>+ ? [T] extends [PropType<any> | { type: PropType<any> }]+ ? V // explicit runtime type: V is inferred from it (also when generic)+ : unknown extends V+ ? keyof V extends never+ ? IfAny<V, V, D>+ : V : V- : V : T /**
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