From f5186910f9cc7bf7d1b94b5a0364ab67cbf18644 Mon Sep 17 00:00:00 2001 From: Julian Schleemann Date: Thu, 8 Feb 2024 18:52:25 +0100 Subject: [PATCH] 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