From 63f12e58661c2a984d7338bb33b277f64b9b5ab9 Mon Sep 17 00:00:00 2001 From: David Matter Date: Tue, 17 Oct 2023 21:34:53 +0200 Subject: [PATCH 1/2] fix(types): inference for generic object types --- packages/dts-test/setupHelpers.test-d.ts | 19 ++++++++++++++++++- packages/runtime-core/src/componentProps.ts | 21 +++++++++++++++------ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index feb4085dea0..a18e6678141 100644 --- a/packages/dts-test/setupHelpers.test-d.ts +++ b/packages/dts-test/setupHelpers.test-d.ts @@ -8,7 +8,8 @@ import { defineSlots, VNode, Ref, - defineModel + defineModel, + PropType } from 'vue' import { describe, expectType } from './utils' import { defineComponent } from 'vue' @@ -40,6 +41,22 @@ describe('defineProps w/ generics', () => { test() }) +//github.com/vuejs/core/issues/9277 +describe('defineProps w/ generic object type', () => { + type Props = { + modelValue: T + } + + const props = defineProps({ + modelValue: { + type: Object as PropType['modelValue']>, + required: true + } + }) + + expectType(props.modelValue) +}) + describe('defineProps w/ type declaration + withDefaults', () => { const res = withDefaults( defineProps<{ diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 4d402789555..3e913b93d37 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -21,8 +21,7 @@ import { EMPTY_ARR, def, extend, - isOn, - IfAny + isOn } from '@vue/shared' import { warn } from './warning' import { @@ -122,10 +121,20 @@ type InferPropType = [T] extends [null] ? U extends DateConstructor ? Date | InferPropType : InferPropType - : [T] extends [Prop] - ? unknown extends V - ? IfAny - : V + : [T] extends [{ type: infer U; default: infer D }] + ? [T] extends [PropOptions] + ? V + : D + : [T] extends [{ type: infer U }] + ? [T] extends [PropOptions] + ? V + : U + : [T] extends [{ default: infer U }] + ? [T] extends [PropOptions] + ? D + : U + : [T] extends [PropType] + ? V : T /** From 4264376a2ef3d2821c46e3f148d4e5823db35fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Thu, 19 Oct 2023 17:24:45 +0000 Subject: [PATCH 2/2] Update packages/dts-test/setupHelpers.test-d.ts --- packages/dts-test/setupHelpers.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dts-test/setupHelpers.test-d.ts b/packages/dts-test/setupHelpers.test-d.ts index a18e6678141..283ffcd0b57 100644 --- a/packages/dts-test/setupHelpers.test-d.ts +++ b/packages/dts-test/setupHelpers.test-d.ts @@ -41,7 +41,7 @@ describe('defineProps w/ generics', () => { test() }) -//github.com/vuejs/core/issues/9277 +// #9277 describe('defineProps w/ generic object type', () => { type Props = { modelValue: T