Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Last active July 21, 2023 07:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NetanelBasal/26395c1e1dca3712ab9ecb6c99ac23d4 to your computer and use it in GitHub Desktop.
Save NetanelBasal/26395c1e1dca3712ab9ecb6c99ac23d4 to your computer and use it in GitHub Desktop.
interface Profile {
firstName: string;
lastName: string;
address: {
street: string;
city: string
}
}
const profileForm = new FormGroup<ControlsOf<Profile>>({
firstName: new FormControl('', { nonNullable: true }),
lastName: new FormControl('', { nonNullable: true }),
address: new FormGroup({
street: new FormControl('', { nonNullable: true }),
city: new FormControl('', { nonNullable: true })
})
});
export type ControlsOf<T extends Record<string, any>> = {
[K in keyof T]: T[K] extends Record<any, any>
? FormGroup<ControlsOf<T[K]>>
: FormControl<T[K]>;
};
@konstantintieber
Copy link

Thanks for sharing this ControlsOf type.
Do you know a way to make it work if the Profile interface had an additional optional control state?

So like this:

interface Profile {
  firstName: string;
  lastName: string;
  address: {
    street: string;
    city: string;
    state?: string | null
  }
}

@Adrek
Copy link

Adrek commented Apr 24, 2023

Thanks for the contribution. I do very well with most forms

but if I need one of the fields to be a FormArray? how would it be?

@kzimny
Copy link

kzimny commented Jul 21, 2023

Thanks for sharing this ControlsOf type. Do you know a way to make it work if the Profile interface had an additional optional control state?

So like this:

interface Profile {
  firstName: string;
  lastName: string;
  address: {
    street: string;
    city: string;
    state?: string | null
  }
}

Has anyone found a solution? How to use ControlsOf(...) with optional controls?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment