From ac127e34a7f67e4174dd10369f645e4bc1a4ad4d Mon Sep 17 00:00:00 2001 From: tycho Date: Tue, 5 Nov 2024 15:45:26 +0800 Subject: [PATCH 1/3] fix(types): avoid marking `PropType as undefined in props --- packages-private/dts-test/setupHelpers.test-d.ts | 3 +++ packages/runtime-core/src/componentProps.ts | 1 + 2 files changed, 4 insertions(+) diff --git a/packages-private/dts-test/setupHelpers.test-d.ts b/packages-private/dts-test/setupHelpers.test-d.ts index 7b5d6f147f0..4e150adaf92 100644 --- a/packages-private/dts-test/setupHelpers.test-d.ts +++ b/packages-private/dts-test/setupHelpers.test-d.ts @@ -1,4 +1,5 @@ import { + type PropType, type Ref, type Slots, type VNode, @@ -269,11 +270,13 @@ describe('defineProps w/ runtime declaration', () => { type: Array, required: true, }, + b1: Boolean as PropType, }) expectType<{ foo?: string bar: number baz: unknown[] + b1: boolean }>(props) props.foo && props.foo + 'bar' diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 8baa7808665..df97b293a26 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -86,6 +86,7 @@ type RequiredKeys = { | { default: any } // don't mark Boolean props as undefined | BooleanConstructor + | PropType | { type: BooleanConstructor } ? T[K] extends { default: undefined | (() => undefined) } ? never From 1612b4a79c2fbff9f5fedc943d66c5d3edbc546d Mon Sep 17 00:00:00 2001 From: tycho Date: Tue, 5 Nov 2024 16:31:12 +0800 Subject: [PATCH 2/3] fix: handle PropType w/ any --- packages-private/dts-test/setupHelpers.test-d.ts | 4 ++++ packages/runtime-core/src/componentProps.ts | 5 +++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages-private/dts-test/setupHelpers.test-d.ts b/packages-private/dts-test/setupHelpers.test-d.ts index 4e150adaf92..4c58e18a908 100644 --- a/packages-private/dts-test/setupHelpers.test-d.ts +++ b/packages-private/dts-test/setupHelpers.test-d.ts @@ -271,12 +271,16 @@ describe('defineProps w/ runtime declaration', () => { required: true, }, b1: Boolean as PropType, + b2: Boolean as PropType, + b3: Number as PropType, }) expectType<{ foo?: string bar: number baz: unknown[] b1: boolean + b2?: any + b3?: number }>(props) props.foo && props.foo + 'bar' diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index df97b293a26..8783dfda074 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -86,12 +86,13 @@ type RequiredKeys = { | { default: any } // don't mark Boolean props as undefined | BooleanConstructor - | PropType | { type: BooleanConstructor } ? T[K] extends { default: undefined | (() => undefined) } ? never : K - : never + : T[K] extends PropType + ? IfAny + : never }[keyof T] type OptionalKeys = Exclude> From 96b0dc2e104dd667e85d55db4784dafccb6965d7 Mon Sep 17 00:00:00 2001 From: tycho Date: Tue, 5 Nov 2024 20:23:28 +0800 Subject: [PATCH 3/3] fix: object props --- .../dts-test/defineComponent.test-d.tsx | 19 +++++++++++++++++++ packages/runtime-core/src/componentProps.ts | 8 ++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages-private/dts-test/defineComponent.test-d.tsx b/packages-private/dts-test/defineComponent.test-d.tsx index fda3ca4856c..c50b08490b1 100644 --- a/packages-private/dts-test/defineComponent.test-d.tsx +++ b/packages-private/dts-test/defineComponent.test-d.tsx @@ -1841,6 +1841,25 @@ defineComponent({ }, }) +describe('object props w/ PropType', () => { + const MyComponent = defineComponent({ + props: { + b1: { type: [Boolean] as PropType }, + b2: { + type: [Boolean, String] as PropType, + required: true, + }, + }, + setup(props) { + expectType(props.b1) + expectType(props.b2) + }, + }) + expectType() + // @ts-expect-error + ; +}) + import type * as vue from 'vue' interface ErrorMessageSlotProps { diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 8783dfda074..ecd5ea41075 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -90,7 +90,7 @@ type RequiredKeys = { ? T[K] extends { default: undefined | (() => undefined) } ? never : K - : T[K] extends PropType + : T[K] extends PropType | { type: PropType } ? IfAny : never }[keyof T] @@ -106,7 +106,11 @@ type DefaultKeys = { ? T[K] extends { type: BooleanConstructor; required: true } // not default if Boolean is marked as required ? never : K - : never + : T[K] extends + | PropType + | { type: PropType; required?: false } + ? IfAny + : never }[keyof T] type InferPropType = [T] extends [null]