Last active
August 27, 2023 00:22
-
-
Save guiseek/11a42ca66b61cd8569abcf7081cd7093 to your computer and use it in GitHub Desktop.
Angular Recursive TypedForm
This file contains hidden or 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
import type {TypedForm} from './typed-form' | |
type TypedFormModel<T> = { | |
// Verifique cada tipo do model | |
[K in keyof T]: TypedForm<T[K]> | |
} | |
export type {TypedFormModel} |
This file contains hidden or 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
import type {FormArray, FormControl, FormGroup} from '@angular/forms' | |
type TypedForm<T> = | |
// Caso T seja um array | |
T extends Array<infer U> | |
// retorne U | |
? FormArray< | |
// e U seja um objeto | |
U extends object | |
// inicie a verificacao para U | |
? FormGroup<TypedFormModel<U>> | |
// ou retorne U | |
: FormControl<U> | |
> | |
// caso seja objeto | |
: T extends object | |
// inicie a verificação para T | |
? FormGroup<TypedFormModel<T>> | |
// caso seja boleano | |
: T extends boolean | |
// retorne boolean | |
? FormControl<boolean> | |
// caso seja string | |
: T extends string | |
// retorne string | |
? FormControl<T | string> | |
// caso seja número | |
: T extends number | |
// retorne número | |
? FormControl<number> | |
// ou retorne T | |
: FormControl<T> | |
export type {TypedForm} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment