Last active
July 21, 2023 07:14
-
-
Save NetanelBasal/26395c1e1dca3712ab9ecb6c99ac23d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>; | |
}; |
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?
Thanks for sharing this
ControlsOf
type. Do you know a way to make it work if theProfile
interface had an additional optional controlstate
?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
Thanks for sharing this
ControlsOf
type.Do you know a way to make it work if the
Profile
interface had an additional optional controlstate
?So like this: