From 2807a8bc686e2477db3bfb0c113d58fad749117c Mon Sep 17 00:00:00 2001 From: Scott Bedard Date: Fri, 5 Mar 2021 10:07:07 -0700 Subject: [PATCH 1/4] Don't treat prop as required if a default is provided Closes #654 --- src/component/componentProps.ts | 4 +++- test-dts/defineComponent.test-d.ts | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/component/componentProps.ts b/src/component/componentProps.ts index d01ee3ed..2c1f0f25 100644 --- a/src/component/componentProps.ts +++ b/src/component/componentProps.ts @@ -27,7 +27,9 @@ type PropConstructor = | { new (...args: string[]): Function } type RequiredKeys = { - [K in keyof T]: T[K] extends { required: true } | { default: any } ? K : never + [K in keyof T]: T[K] extends { required: true } | { default: never } + ? K + : never }[keyof T] type OptionalKeys = Exclude> diff --git a/test-dts/defineComponent.test-d.ts b/test-dts/defineComponent.test-d.ts index bdd033cc..3481e274 100644 --- a/test-dts/defineComponent.test-d.ts +++ b/test-dts/defineComponent.test-d.ts @@ -13,8 +13,8 @@ describe('with object props', () => { a?: number | undefined b: string e?: Function - bb: string - bbb: string + bb?: string + bbb?: string cc?: string[] | undefined dd: { n: 1 } ee?: () => string @@ -24,8 +24,8 @@ describe('with object props', () => { eee: () => { a: string } fff: (a: number, b: string) => { a: boolean } hhh: boolean - ggg: 'foo' | 'bar' - ffff: (a: number, b: string) => { a: boolean } + ggg?: 'foo' | 'bar' + ffff?: (a: number, b: string) => { a: boolean } validated?: string date: Date } From 3d44c293e987d225b2cc6e71f7ce9654d3493089 Mon Sep 17 00:00:00 2001 From: Scott Bedard Date: Fri, 5 Mar 2021 10:08:54 -0700 Subject: [PATCH 2/4] Revert accidental whitespace change --- src/component/componentProps.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/component/componentProps.ts b/src/component/componentProps.ts index 2c1f0f25..85199e79 100644 --- a/src/component/componentProps.ts +++ b/src/component/componentProps.ts @@ -27,9 +27,7 @@ type PropConstructor = | { new (...args: string[]): Function } type RequiredKeys = { - [K in keyof T]: T[K] extends { required: true } | { default: never } - ? K - : never + [K in keyof T]: T[K] extends { required: true } | { default: never } ? K : never }[keyof T] type OptionalKeys = Exclude> From f8099eaef7ca20fc85f32eae4c267fe0a1897617 Mon Sep 17 00:00:00 2001 From: Scott Bedard Date: Mon, 8 Mar 2021 20:17:54 -0600 Subject: [PATCH 3/4] these are made optional by presence of default value --- test/types/defineComponent.spec.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/types/defineComponent.spec.ts b/test/types/defineComponent.spec.ts index c41e08b1..4caa516c 100644 --- a/test/types/defineComponent.spec.ts +++ b/test/types/defineComponent.spec.ts @@ -58,8 +58,8 @@ describe('defineComponent', () => { setup(props, ctx) { type PropsType = typeof props isTypeEqual(true) - isSubType(true) - isSubType<{ readonly b?: string; readonly a: number }, PropsType>(true) + isSubType(true) + isSubType<{ readonly b?: string; readonly a?: number }, PropsType>(true) return () => null }, }) @@ -191,12 +191,16 @@ describe('defineComponent', () => { setup(props) { type PropsType = typeof props isSubType< - { readonly foo: string; readonly bar: string; readonly zoo?: string }, + { + readonly foo: string + readonly bar?: string + readonly zoo?: string + }, PropsType >(true) isSubType< PropsType, - { readonly foo: string; readonly bar: string; readonly zoo?: string } + { readonly foo: string; readonly bar?: string; readonly zoo?: string } >(true) return () => null }, From 46752730cbe6ee4cb547229c6b4788806ce1a73d Mon Sep 17 00:00:00 2001 From: Scott Bedard Date: Mon, 8 Mar 2021 20:21:53 -0600 Subject: [PATCH 4/4] Revert more accidental whitespace changes --- test/types/defineComponent.spec.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/types/defineComponent.spec.ts b/test/types/defineComponent.spec.ts index 4caa516c..d35d6281 100644 --- a/test/types/defineComponent.spec.ts +++ b/test/types/defineComponent.spec.ts @@ -191,11 +191,7 @@ describe('defineComponent', () => { setup(props) { type PropsType = typeof props isSubType< - { - readonly foo: string - readonly bar?: string - readonly zoo?: string - }, + { readonly foo: string; readonly bar?: string; readonly zoo?: string }, PropsType >(true) isSubType<