Skip to content

Commit

Permalink
feat(lib): formGroupControls property now gives you access to the c…
Browse files Browse the repository at this point in the history
…orrect type without the need to cast it (either `FormControl` or `FormArray`)
  • Loading branch information
maxime1992 committed Jun 23, 2019
1 parent c89ba94 commit ddfcd5a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions projects/ngx-sub-form/src/lib/ngx-sub-form-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type ControlsNames<T> = { [K in keyof T]-?: K };

export type ControlMap<T, V> = { [K in keyof T]-?: V };

export type ControlsType<T> = { [K in keyof T]-?: T[K] extends any[] ? FormArray : FormControl };

export type FormUpdate<FormInterface> = { [FormControlInterface in keyof FormInterface]?: true };

export type FormErrors<FormInterface> = null | Partial<
Expand Down
5 changes: 3 additions & 2 deletions projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
MissingFormControlsError,
FormErrors,
isNullOrUndefined,
ControlsType,
} from './ngx-sub-form-utils';
import { FormGroupOptions, OnFormUpdate, TypedFormGroup } from './ngx-sub-form.types';

Expand All @@ -27,11 +28,11 @@ type FilterControlFunction<FormInterface> = (ctrl: AbstractControl, key: keyof F

export abstract class NgxSubFormComponent<ControlInterface, FormInterface = ControlInterface>
implements ControlValueAccessor, Validator, OnDestroy, OnFormUpdate<FormInterface> {
public get formGroupControls(): ControlMap<FormInterface, AbstractControl> {
public get formGroupControls(): ControlsType<FormInterface> {
// @note form-group-undefined we need the no-null-assertion here because we do not want to expose the fact that
// the form can be undefined, it's handled internally to contain an Angular bug
// tslint:disable-next-line:no-non-null-assertion
return this.mapControls<AbstractControl>()!;
return this.mapControls<any>()!;
}

public get formGroupValues(): Required<FormInterface> {
Expand Down

0 comments on commit ddfcd5a

Please sign in to comment.