Skip to content

Commit

Permalink
Merge pull request #6 from atmina/nested-optional-nullable
Browse files Browse the repository at this point in the history
feat: allow drilling through optional and nullable
  • Loading branch information
mvarendorff2 authored Feb 9, 2024
2 parents 3084bce + 050e973 commit 972a23d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/formbuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export type FormBuilder<T> = FormBuilderRegisterFn<T> & {
): $UseFieldArrayReturn<TItem>;
}
: {
[K in Exclude<keyof T, `${"__"}${string}`>]-?: FormBuilder<T[K]>;
[K in Exclude<keyof T, `${"__"}${string}`>]-?: FormBuilder<
T[K] extends Primitive ? T[K] : NonNullable<T[K]>
>;
});

/*
Expand Down
16 changes: 16 additions & 0 deletions src/types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ describe("Types", () => {
FormBuilder<{ foo?: string }>
>();

// Nested optional
type NestedOptional = FormBuilder<{
foo?: { bar?: { baz?: string } };
}>;
expectTypeOf<NestedOptional["foo"]["bar"]["baz"]>().toMatchTypeOf<
FormBuilder<string>
>();

// Nullable
expectTypeOf<FormBuilder<string | null>>().toMatchTypeOf<
FormBuilder<string>
Expand All @@ -46,6 +54,14 @@ describe("Types", () => {
FormBuilder<string | null>
>();

// Nested nullable
type NestedNull = FormBuilder<{
foo: { bar: { baz: string | null } | null } | null;
}>;
expectTypeOf<NestedNull["foo"]["bar"]["baz"]>().toMatchTypeOf<
FormBuilder<string | null>
>();

// Undefined
expectTypeOf<FormBuilder<string | undefined>>().toMatchTypeOf<
FormBuilder<string>
Expand Down

0 comments on commit 972a23d

Please sign in to comment.