Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ControlsType): added typings for AbstractControls + FormArrays #139

Merged
merged 16 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion projects/ngx-sub-form/src/lib/ngx-sub-form-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ 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 : AbstractControl };
export type ControlsType<T> = {
[K in keyof T]-?: T[K] extends any[] ? TypedFormArray<T[K]> : TypedAbstractControl<T[K]>;
};
export type FormErrorsType<T> = {
[K in keyof T]-?: T[K] extends any[] ? (null | ValidationErrors)[] : ValidationErrors;
};
Expand All @@ -32,6 +34,37 @@ export type FormErrors<FormInterface> = null | Partial<
}
>;

type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; // can be removed when upgrading ts version
type UntypedControlBase<TControl extends AbstractControl | FormArray> = Omit<
TControl,
'value' | 'valueChanges' | 'setValue' | 'patchValue'
>;

export type TypedAbstractControl<TValue extends any> = {
ntziolis marked this conversation as resolved.
Show resolved Hide resolved
value: TValue;
valueChanges: Observable<TValue>;
setValue(
value: TValue,
options?: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
},
ntziolis marked this conversation as resolved.
Show resolved Hide resolved
): void;
patchValue(
value: Partial<TValue>,
options?: {
onlySelf?: boolean;
emitEvent?: boolean;
emitModelToViewChange?: boolean;
emitViewToModelChange?: boolean;
ntziolis marked this conversation as resolved.
Show resolved Hide resolved
},
): void;
} & UntypedControlBase<AbstractControl>;

export type TypedFormArray<TValue extends any> = TypedAbstractControl<TValue> & UntypedControlBase<FormArray>;
ntziolis marked this conversation as resolved.
Show resolved Hide resolved

export type KeysWithType<T, V> = { [K in keyof T]: T[K] extends V ? K : never }[keyof T];

export type ArrayPropertyKey<T> = KeysWithType<T, Array<any>>;
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-sub-form/src/lib/ngx-sub-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export abstract class NgxSubFormComponent<ControlInterface, FormInterface = Cont
return null as any;
}

return this.formGroup.controls as ControlsType<FormInterface>;
return this.formGroup.controls as unknown as ControlsType<FormInterface>;
}

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