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", 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..f13f252 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