From f5186910f9cc7bf7d1b94b5a0364ab67cbf18644 Mon Sep 17 00:00:00 2001 From: Julian Schleemann Date: Thu, 8 Feb 2024 18:52:25 +0100 Subject: [PATCH 1/3] feat: allow drilling through optional and nullable --- src/formbuilder.tsx | 4 +++- src/types.test.tsx | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/formbuilder.tsx b/src/formbuilder.tsx index 339930c..fbdd31b 100644 --- a/src/formbuilder.tsx +++ b/src/formbuilder.tsx @@ -81,7 +81,9 @@ export type FormBuilder = FormBuilderRegisterFn & { ): $UseFieldArrayReturn; } : { - [K in Exclude]-?: FormBuilder; + [K in Exclude]-?: FormBuilder< + T[K] extends Primitive ? T[K] : NonNullable + >; }); /* diff --git a/src/types.test.tsx b/src/types.test.tsx index 032be76..26bc99b 100644 --- a/src/types.test.tsx +++ b/src/types.test.tsx @@ -38,6 +38,14 @@ describe("Types", () => { FormBuilder<{ foo?: string }> >(); + // Nested optional + type NestedOptional = FormBuilder<{ + foo?: { bar?: { baz?: string } }; + }>; + expectTypeOf().toMatchTypeOf< + FormBuilder + >(); + // Nullable expectTypeOf>().toMatchTypeOf< FormBuilder @@ -46,6 +54,14 @@ describe("Types", () => { FormBuilder >(); + // Nested nullable + type NestedNull = FormBuilder<{ + foo: { bar: { baz: string | null } | null } | null; + }>; + expectTypeOf().toMatchTypeOf< + FormBuilder + >(); + // Undefined expectTypeOf>().toMatchTypeOf< FormBuilder From 343277424ecd55f55693093661b805338b336231 Mon Sep 17 00:00:00 2001 From: Julian Schleemann Date: Thu, 8 Feb 2024 18:55:47 +0100 Subject: [PATCH 2/3] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b76940a..683fdb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@atmina/formbuilder", - "version": "0.0.6", + "version": "0.0.7", "description": "A strongly-typed alternative API for React Hook Form.", "source": "src/index.ts", "main": "lib/index.js", From 050e97387ee28d2db00b31ca9e5e4567da045782 Mon Sep 17 00:00:00 2001 From: reiv Date: Thu, 8 Feb 2024 20:08:24 +0100 Subject: [PATCH 3/3] update types test --- src/types.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.test.tsx b/src/types.test.tsx index 26bc99b..f13f252 100644 --- a/src/types.test.tsx +++ b/src/types.test.tsx @@ -59,7 +59,7 @@ describe("Types", () => { foo: { bar: { baz: string | null } | null } | null; }>; expectTypeOf().toMatchTypeOf< - FormBuilder + FormBuilder >(); // Undefined