Skip to content

Instantly share code, notes, and snippets.

@guiseek
Last active August 27, 2023 00:22
Show Gist options
  • Save guiseek/11a42ca66b61cd8569abcf7081cd7093 to your computer and use it in GitHub Desktop.
Save guiseek/11a42ca66b61cd8569abcf7081cd7093 to your computer and use it in GitHub Desktop.
Angular Recursive TypedForm
import type {TypedForm} from './typed-form'
type TypedFormModel<T> = {
// Verifique cada tipo do model
[K in keyof T]: TypedForm<T[K]>
}
export type {TypedFormModel}
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