import { AbstractControl, AbstractControlOptions, AsyncValidatorFn, UntypedFormArray, UntypedFormControl, UntypedFormGroup, ValidatorFn } from '@angular/forms'; import { Observable } from 'rxjs'; /** * Type encapsulating the Angular Form options: * `emitEvent` - do we emit event; * `onlySelf` - do we bubble up to parent * `emitModelToViewChange` - When true or not supplied (the default), each change triggers an onChange event to update the view. * `emitViewToModelChange` - When true or not supplied (the default), each change triggers an ngModelChange event to update the model. */ export interface FormEventOptions { emitEvent?: boolean; onlySelf?: boolean; emitModelToViewChange?: boolean; emitViewToModelChange?: boolean; } /** * A type wrapper around the reset value. It could be partial of the type of the form. Or even describe which form fields are to be disabled */ export type ResetValue = Partial<{ [key in keyof K]: K[key] | { value: K[key]; disabled: boolean; }; }>; /** * Typed form control is an `AbstractControl` with strong typed properties and methods. Can be created using `typedFormControl` function * * @example * const c = typedFormControl(): TypedFormControl; * c.valueChanges // Observable * c.patchValue('s') // expects string * c.patchValue(1) // COMPILE TIME! type error! */ export interface TypedFormControl extends UntypedFormControl, AbstractControl { valueChanges: Observable; statusChanges: Observable<'VALID' | 'INVALID' | 'PENDING' | 'DISABLED'>; patchValue: (v: Partial, options?: FormEventOptions) => void; setValue: (v: K, options?: FormEventOptions) => void; value: K; reset: (value?: ResetValue, opts?: FormEventOptions) => void; } /** * A helper function to create a `TypedFormControl`. It only calls the constructor of FormControl, but **strongly types** the result. * @param v the value to initialize our `TypedFormControl` with - same as in `new FormControl(v, validators, asyncValidators)` * @param validators validators - same as in new `FormControl(v, validators, asyncValidators)` * @param asyncValidators async validators - same as in `new FormControl(v, validators, asyncValidators)` * * @example * const c = typedFormControl(): TypedFormControl; * c.valueChanges // Observable * c.patchValue('s') // expects string * c.patchValue(1) // COMPILE TIME! type error! */ export declare function typedFormControl(v?: T | { value: T; disabled: boolean; }, validatorsOrOptions?: ValidatorFn | ValidatorFn[] | AbstractControlOptions, asyncValidators?: AsyncValidatorFn | AsyncValidatorFn[]): TypedFormControl; /** * Typed form control is an `FormArray` with strong typed properties and methods. Can be created using `typedFormArray` function * * @example * const c = typedFormArray([typedFormControl('of type string')]): TypedFormArray; * c.valueChanges // Observable * c.patchValue(['s']) // expects string[] * c.patchValue(1) // COMPILE TIME! type error! */ export interface TypedFormArray = any[], T = any> extends UntypedFormArray { valueChanges: Observable; statusChanges: Observable<'VALID' | 'INVALID' | 'PENDING' | 'DISABLED'>; patchValue: (v: K, options?: FormEventOptions) => void; setValue: (v: K, options?: FormEventOptions) => void; controls: Array>; push: (control: TypedFormControl) => void; insert: (index: number, control: TypedFormControl) => void; at: (index: number) => TypedFormControl; setControl: (index: number, control: TypedFormControl) => void; value: K; } /** * A helper function to create a `TypedFormArray`. It only calls the constructor of FormArray, but **strongly types** the result. * @param v the value to initialize our `TypedFormArray` with - same as in `new TypedFormArray(v, validators, asyncValidators)` * @param validators validators - same as in new `TypedFormArray(v, validators, asyncValidators)` * @param asyncValidators async validators - same as in `new TypedFormArray(v, validators, asyncValidators)` * * @example * const c = typedFormArray([typedFormControl('of type string')]): TypedFormArray; * c.valueChanges // Observable * c.patchValue(['s']) // expects string[] * c.patchValue(1) // COMPILE TIME! type error! */ export declare function typedFormArray = T[]>(controls: Array>, validatorOrOptions?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidators?: AsyncValidatorFn | AsyncValidatorFn[] | null): TypedFormArray; export type Controls = TypedControlsIn | { [k in keyof K]: K[k] extends Array ? TypedFormControl | TypedFormGroup | TypedFormArray : TypedFormControl | TypedFormGroup; }; type NonGroup = string | number | boolean | Function | null | undefined | never; /** * This is a strongly typed thin wrapper type around `FormGroup`. * Can be created using the `typedFormGroup` function */ export type TypedFormGroup = TypedControlsIn> = K extends NonGroup ? never : UntypedFormGroup & TypedFormControl & { controls: K extends NonGroup ? never : C; valueChanges: Observable; statusChanges: Observable<'VALID' | 'INVALID' | 'PENDING' | 'DISABLED'>; patchValue: (v: Partial, options?: FormEventOptions) => void; setValue: (v: K, options?: FormEventOptions) => void; value: K; setControl: (name: T extends string ? T : never, control: C[T]) => void; reset: (value?: ResetValue, options?: FormEventOptions) => void; getRawValue: () => K; }; export declare function typedFormGroup = TypedControlsIn, Key extends keyof K = keyof K>(controls: K extends NonGroup ? never : C, validatorOrOpts?: ValidatorFn | ValidatorFn[] | AbstractControlOptions | null, asyncValidator?: AsyncValidatorFn | AsyncValidatorFn[] | null): TypedFormGroup & { keys: Record; }; /** * Helper type for specifying what control we expect for each property from the model. */ export type TypedControlsIn = { [key in keyof K]-?: key extends groups ? TypedFormGroup : key extends arrays ? K[key] extends Array ? TypedFormArray : never : TypedFormControl; }; /** * Shorthand for a model with `TypedFormControl`s and `TypedFormArray`s only (i.e. no `TypedFormGroup`s in) */ export type TypedArraysIn = TypedControlsIn; /** * Shorthand for a model with `TypedFormControl`s and `TypedFormGroup`s only (i.e. no `TypedFormArray`s in) */ export type TypedGroupsIn = TypedControlsIn; export {};