Skip to content

Commit

Permalink
feat: allow drilling through optional and nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Schleemann committed Feb 8, 2024
1 parent 3084bce commit f518691
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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>
>();

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

0 comments on commit f518691

Please sign in to comment.