From 3e90e06eeed0d60062e757639448c80f5fbdf0da Mon Sep 17 00:00:00 2001 From: Pier Fumagalli Date: Tue, 28 Nov 2023 18:54:30 +0100 Subject: [PATCH] fix(runtime-core): incomplete type inferred by generic components --- packages/dts-test/setupHelpers.test-d.ts | 20 ++++++++++++++++++++ packages/runtime-core/src/componentProps.ts | 16 +++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index 51f95c00944..f09e9029550 100644 --- a/packages/dts-test/setupHelpers.test-d.ts +++ b/packages/dts-test/setupHelpers.test-d.ts @@ -4,6 +4,7 @@ import { useAttrs, useSlots, withDefaults, + PropType, Slots, defineSlots, VNode, @@ -193,6 +194,25 @@ describe('defineProps w/ runtime declaration', () => { props2.baz }) +describe('defineProps w/ generics and runtime declarations', () => { + function test>() { + const props = defineProps({ + foo: { + type: Object as PropType, + required: false, + default: null + }, + bar: { + type: Object as PropType, + required: true + } + }) + expectType(props.foo) + expectType(props.bar) + } + test() +}) + describe('defineEmits w/ type declaration', () => { const emit = defineEmits<(e: 'change') => void>() emit('change') diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index b8e9bf66db5..93a459b09a6 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -122,11 +122,17 @@ type InferPropType = [T] extends [null] ? U extends DateConstructor ? Date | InferPropType : InferPropType - : [T] extends [Prop] - ? unknown extends V - ? IfAny - : V - : T + : [T] extends [PropOptions] + ? unknown extends D + ? V + : unknown extends V + ? IfAny + : V + : [T] extends [Prop] + ? unknown extends V + ? IfAny + : V + : T /** * Extract prop types from a runtime props options object.