Backtestsvuerun-20261005-124535#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 target the root cause: when a prop option references a generic `T`, the key-classification conditionals are deferred, so optional props get dropped. They fix it in different places.
Patch B changes only the key-classification helpers. It wraps the check as `{ v: T[K] } extends { v: ... }`, which keeps TypeScript from deferring the conditional on a generic check type. It also computes `OptionalKeys` and `PublicOptionalKeys` per key instead of via `Exclude<keyof T, RequiredKeys<T>>`, which stays deferred when any key is unresolved. The same treatment goes to `DefaultKeys` and `PublicRequiredKeys`. This is local and does not change what `PropType<X>` means for any existing user code. The comments explain the trick clearly.
Patch A takes a wider path:
- It removes the conditional from `PropMethod`, so every `PropType<X>` now includes a function-like constructor member, not just function types. That conditional existed on purpose for function props, and removing it alters the shape of a central public type.
- It also adds a new `unknown extends D ? V : D` branch in `InferPropType`.
These changes are probably mostly harmless, but their effect reaches beyond the bug and is harder to reason about.
On tests, Patch A has the stronger value assertions: `T | null`, `T`, `T | undefined`, plus a usability check. Patch B's `expectType<unknown>` value checks are nearly tautological. However, Patch B checks what the issue is actually about: the full key set, which keys are required or optional (with `@ts-expect-error` negatives), and correct handling of `ExtractPublicPropTypes` and `ExtractDefaultPropTypes` under generics. Patch A does not cover those. Patch B also includes non-generic props alongside generic ones, confirming that `Boolean`/`default`/`String` behavior is preserved.
Weighing correctness and risk first, Patch B's fix is narrower and safer and addresses the dropped-keys problem directly across all the extract helpers. Patch A's better value-type assertions do not outweigh its riskier change to `PropType`.
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
36m 29s
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
168vs 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..2d9825fed 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,70 @@ expectType<number | undefined>(publicProps.foo) expectType<string>(publicProps.bar) expectType<boolean | undefined>(publicProps.baz) expectType<unknown[] | undefined>(publicProps.qux)++// #9546+describe('props options referencing a generic type parameter', () => {+ function test<T>() {+ const genericOptions = {+ a: Object as PropType<T>,+ b: {+ type: Object as PropType<T>,+ },+ c: {+ type: Object as PropType<T>,+ required: true,+ },+ d: String,+ e: Boolean,+ f: {+ type: Number,+ default: 1,+ },+ } as const++ type Keys = 'a' | 'b' | 'c' | 'd' | 'e' | 'f'++ // internal facing props+ const props = {} as Prettify<ExtractPropTypes<typeof genericOptions>>+ expectType<Keys>({} as keyof typeof props)+ expectType<unknown>(props.a)+ expectType<unknown>(props.b)+ expectType<unknown>(props.c)+ expectType<string | undefined>(props.d)+ expectType<boolean>(props.e)+ expectType<number>(props.f)+ expectType<{ c: unknown; e: unknown; f: unknown }>(props)+ // @ts-expect-error+ expectType<{ a: unknown }>(props)+ // @ts-expect-error+ expectType<{ b: unknown }>(props)+ // @ts-expect-error+ expectType<{ d: unknown }>(props)++ // external facing props+ const publicProps = {} as Prettify<+ ExtractPublicPropTypes<typeof genericOptions>+ >+ expectType<Keys>({} as keyof typeof publicProps)+ expectType<unknown>(publicProps.a)+ expectType<unknown>(publicProps.b)+ expectType<unknown>(publicProps.c)+ expectType<string | undefined>(publicProps.d)+ expectType<boolean | undefined>(publicProps.e)+ expectType<number | undefined>(publicProps.f)+ expectType<{ c: unknown }>(publicProps)+ // @ts-expect-error+ expectType<{ a: unknown }>(publicProps)+ // @ts-expect-error+ expectType<{ e: unknown }>(publicProps)++ // props with default values+ const defaults = {} as Prettify<+ ExtractDefaultPropTypes<typeof genericOptions>+ >+ expectType<'e' | 'f'>({} as keyof typeof defaults)+ expectType<boolean>(defaults.e)+ expectType<number>(defaults.f)+ }+ test()+})packages-private/dts-test/setupHelpers.test-d.tsindex 067b81b03..f1eb63412 100644@@ -268,6 +268,48 @@ describe('withDefaults w/ defineProp discriminate union type', () => { } }) +describe('defineProps w/ runtime declaration + generics', () => {+ // #9546+ 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,+ },+ d: String,+ e: Boolean,+ f: {+ type: Number,+ default: 1,+ },+ })++ // no props should be dropped+ expectType<unknown>(props.a)+ expectType<unknown>(props.b)+ expectType<unknown>(props.c)+ expectType<string | undefined>(props.d)+ expectType<boolean>(props.e)+ expectType<number>(props.f)+ expectType<'a' | 'b' | 'c' | 'd' | 'e' | 'f'>({} as keyof typeof props)++ // required / with default / Boolean props are always present+ expectType<{ c: unknown; e: unknown; f: unknown }>(props)+ // others are optional+ // @ts-expect-error+ expectType<{ a: unknown }>(props)+ // @ts-expect-error+ expectType<{ b: unknown }>(props)+ // @ts-expect-error+ expectType<{ d: unknown }>(props)+ }+ test()+})+ describe('defineProps w/ runtime declaration', () => { // runtime declaration const props = defineProps({packages/runtime-core/src/componentProps.tsindex 94782e17c..1f52cbb00 100644@@ -78,27 +78,47 @@ type PropMethod<T, TConstructor = any> = [T] extends [ ? { new (): TConstructor; (): T; readonly prototype: TConstructor } // Create Function like constructor : never +type RequiredPropOptions =+ | { required: true }+ | { default: any }+ // don't mark Boolean props as undefined+ | BooleanConstructor+ | { type: BooleanConstructor }++// NOTE: the prop options are checked as `{ v: T[K] } extends { v: ... }`+// rather than `T[K] extends ...`: when the options reference a generic type+// parameter (e.g. `Object as PropType<T>` in `<script setup generic="T">`),+// the latter is deferred and the prop gets dropped from the extracted type.+ type RequiredKeys<T> = {- [K in keyof T]: T[K] extends- | { required: true }- | { default: any }- // don't mark Boolean props as undefined- | BooleanConstructor- | { type: BooleanConstructor }+ [K in keyof T]: { v: T[K] } extends { v: RequiredPropOptions } ? T[K] extends { default: undefined | (() => undefined) } ? never : K : never }[keyof T] -type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>+// computed per key instead of `Exclude<keyof T, RequiredKeys<T>>`, which+// stays deferred (dropping every optional prop) as soon as a single key of+// `RequiredKeys<T>` can't be resolved+type OptionalKeys<T> = {+ [K in keyof T]: { v: T[K] } extends { v: RequiredPropOptions }+ ? IfAny<+ T[K],+ never,+ T[K] extends { default: undefined | (() => undefined) } ? K : never+ >+ : K+}[keyof T] type DefaultKeys<T> = {- [K in keyof T]: T[K] extends- | { default: any }- // Boolean implicitly defaults to false- | BooleanConstructor- | { type: BooleanConstructor }+ [K in keyof T]: { v: T[K] } extends {+ v:+ | { 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 ? never : K@@ -151,10 +171,12 @@ export type ExtractPropTypes<O> = { } type PublicRequiredKeys<T> = {- [K in keyof T]: T[K] extends { required: true } ? K : never+ [K in keyof T]: { v: T[K] } extends { v: { required: true } } ? K : never }[keyof T] -type PublicOptionalKeys<T> = Exclude<keyof T, PublicRequiredKeys<T>>+type PublicOptionalKeys<T> = {+ [K in keyof T]: { v: T[K] } extends { v: { required: 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