Backtestsvuerun-20261007-145226#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 make the same core change: `PropMethod` stops being a conditional on `T`. That removes the deferral of `PropType<T>` that broke `RequiredKeys`/`OptionalKeys` and dropped the non-required props. They differ in `InferPropType`. Patch A keeps the `unknown extends V ? keyof V extends never ? ...` chain and only changes the innermost branch to `unknown extends D ? V : D`. When `V` is a generic type parameter, `unknown extends V` is still deferred. The resulting prop type is an unresolved conditional that happens to be assignable to `V` because every branch is `V`. This hides the problem rather than resolving the type, and property access or narrowing on such a deferred type can behave poorly. Patch B checks `unknown extends D` first. `D` is concrete when no default is given, since it is inferred as `unknown`. So in the common case the result resolves immediately to `V`, with no deferral. The default-fallback logic moves to a separate helper, `InferPropTypeWithDefault`, used only when a default exists. This fixes the root cause more directly. Patch B's tests are also much broader. They cover all three forms from the issue, a `default: undefined` case, array and function `PropType`s with generics, an array default, a plain `String` prop, and property access on the generic (`props.c.foo`, `props.a?.foo`). They also test `ExtractPropTypes`, `ExtractPublicPropTypes` and `ExtractDefaultPropTypes` directly with generic options. Patch A's tests are thinner and lean on assignability, which its deferred-type approach would satisfy anyway. Both patches are tidy and well commented. Patch B has the more robust fix and stronger tests.
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
13m 20s
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
124vs 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..000ea45dd 100644@@ -1,4 +1,9 @@-import type { ExtractPropTypes, ExtractPublicPropTypes } from 'vue'+import type {+ ExtractDefaultPropTypes,+ ExtractPropTypes,+ ExtractPublicPropTypes,+ PropType,+} from 'vue' import { type Prettify, expectType } from './utils' const propsOptions = {@@ -28,3 +33,39 @@ expectType<number | undefined>(publicProps.foo) expectType<string>(publicProps.bar) expectType<boolean | undefined>(publicProps.baz) expectType<unknown[] | undefined>(publicProps.qux)++// generic prop types+export function genericProps<T>(): void {+ type Options = {+ foo: PropType<T>+ bar: { type: PropType<T> }+ baz: { type: PropType<T>; required: true }+ qux: { type: PropType<T[]>; default: () => [] }+ }++ // internal facing props+ const props = {} as Prettify<ExtractPropTypes<Options>>+ expectType<T | undefined>(props.foo)+ expectType<T | undefined>(props.bar)+ expectType<T>(props.baz)+ expectType<T[]>(props.qux)+ // @ts-expect-error optional+ expectType<T>(props.foo)+ // @ts-expect-error optional+ expectType<T>(props.bar)++ // external facing props+ const publicProps = {} as Prettify<ExtractPublicPropTypes<Options>>+ expectType<T | undefined>(publicProps.foo)+ expectType<T | undefined>(publicProps.bar)+ expectType<T>(publicProps.baz)+ expectType<T[] | undefined>(publicProps.qux)+ // @ts-expect-error optional+ expectType<T>(publicProps.foo)++ // default props+ const defaults = {} as ExtractDefaultPropTypes<Options>+ expectType<T[]>(defaults.qux)+ // @ts-expect-error no default+ defaults.foo+}packages-private/dts-test/setupHelpers.test-d.tsindex 067b81b03..fea8913e2 100644@@ -178,6 +178,53 @@ describe('defineProps w/ generic discriminate union + withDefaults', () => { } }) +describe('defineProps w/ runtime declaration + generics', () => {+ function test<T extends { foo: string }>() {+ 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: undefined,+ },+ e: Array as PropType<T[]>,+ f: {+ type: Function as PropType<(value: T) => void>,+ required: true,+ },+ g: {+ type: Array as PropType<T[]>,+ default: () => [],+ },+ h: String,+ })+ expectType<T | undefined>(props.a)+ expectType<T | undefined>(props.b)+ expectType<T>(props.c)+ expectType<string>(props.c.foo)+ expectType<T | undefined>(props.d)+ expectType<T[] | undefined>(props.e)+ expectType<(value: T) => void>(props.f)+ expectType<T[]>(props.g)+ expectType<string | undefined>(props.h)+ expectType<string | undefined>(props.a?.foo)++ // @ts-expect-error not declared+ props.z+ // @ts-expect-error optional+ expectType<T>(props.a)+ // @ts-expect-error optional+ expectType<T>(props.b)+ }+ test()+})+ describe('defineProps w/ generic type declaration + withDefaults', <T extends number, TA extends { a: stringpackages/runtime-core/src/componentProps.tsindex 94782e17c..9e673a57c 100644@@ -72,11 +72,18 @@ 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, allows e.g. `Function as PropType<(a: number) => void>`.+// This must not be a conditional type on `T`: when `T` is a generic type+// parameter (e.g. `<script setup generic="T">`), a conditional would be+// deferred and make every check on the prop options (required / default /+// inferred type) unresolvable, dropping the props from `ExtractPropTypes`.+// For non-function `T` this member is harmless since it is strictly narrower+// than `{ (): T }`.+type PropMethod<T, TConstructor = any> = {+ new (): TConstructor+ (): T+ readonly prototype: TConstructor+} type RequiredKeys<T> = { [K in keyof T]: T[K] extends@@ -122,13 +129,20 @@ 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- : V+ ? // no `default` provided (`D` is exactly `unknown`): use `V` as-is,+ // so a generic `V` (e.g. `PropType<T>`) is not deferred+ unknown extends D+ ? IfAny<D, InferPropTypeWithDefault<V, D>, V>+ : InferPropTypeWithDefault<V, D> : T +// fallback to the type of `default` when the prop type cannot be inferred+type InferPropTypeWithDefault<V, D> = unknown extends V+ ? keyof V extends never+ ? IfAny<V, V, D>+ : V+ : V+ /** * 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