From f6aea2fdbf81a713c3714f498808d7d528c0044f Mon Sep 17 00:00:00 2001 From: "Francesco (Francis) Lodovici" <109726909+francis-switcho@users.noreply.github.com> Date: Fri, 8 Nov 2024 18:47:59 +0000 Subject: [PATCH] fix(types): backport fix from https://github.com/vuejs/core/pull/8335 --- types/test/setup-helpers-test.ts | 34 ++++++++++++++++++ types/v3-setup-helpers.d.ts | 59 ++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 21 deletions(-) diff --git a/types/test/setup-helpers-test.ts b/types/test/setup-helpers-test.ts index 4876154b67d..55d283beab3 100644 --- a/types/test/setup-helpers-test.ts +++ b/types/test/setup-helpers-test.ts @@ -98,6 +98,40 @@ describe('defineProps w/ runtime declaration', () => { props2.baz }) +describe('defineProps w/ generic type declaration + withDefaults', () => { + const res = withDefaults( + defineProps<{ + n?: number + bool?: boolean + + generic1?: T[] | { x: T } + generic2?: { x: T } + generic3?: TString + generic4?: TA + }>(), + { + n: 123, + + generic1: () => [123, 33] as T[], + generic2: () => ({ x: 123 } as { x: T }), + + generic3: () => 'test' as TString, + generic4: () => ({ a: 'test' } as TA) + } + ) + + res.n + 1 + + expectType(res.generic1) + expectType<{ x: T }>(res.generic2) + expectType(res.generic3) + expectType(res.generic4) + + expectType(res.bool) +}) + describe('defineEmits w/ type declaration', () => { const emit = defineEmits<(e: 'change') => void>() emit('change') diff --git a/types/v3-setup-helpers.d.ts b/types/v3-setup-helpers.d.ts index 165605ee51b..ce255a6dbad 100644 --- a/types/v3-setup-helpers.d.ts +++ b/types/v3-setup-helpers.d.ts @@ -43,7 +43,20 @@ export function defineProps< PP extends ComponentObjectPropsOptions = ComponentObjectPropsOptions >(props: PP): Readonly> // overload 3: typed-based declaration -export function defineProps(): Readonly +export function defineProps(): DefineProps< + TypeProps, + BooleanKey +> + +type DefineProps = Readonly & { + readonly [K in BKeys]-?: boolean +} + +type BooleanKey = K extends any + ? [T[K]] extends [boolean | undefined] + ? K + : never + : never /** * Vue `