Skip to content

Commit 7df9127

Browse files
dylhunnalxhub
authored andcommitted
refactor(forms): Fix weak helper types in the Forms package, mostly Function types that can be strengthened to specific callbacks. This was originally proposed as part of Typed Forms, but it may be possible to submit at least some of these changes separately and unconditionally (i.e. without relying on TypedOrUntyped.) (#44370)
It is desirable to land this separately to reduce the scope of the Typed Forms PR, by focusing it only on the new type parameters (rather than incidental strictness fixes). PR Close #44370
1 parent 78bdb1f commit 7df9127

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

packages/forms/src/directives/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ function updateControl(control: FormControl, dir: NgControl): void {
226226
}
227227

228228
function setUpModelChangePipeline(control: FormControl, dir: NgControl): void {
229-
const onChange = (newValue: any, emitModelEvent: boolean) => {
229+
const onChange = (newValue?: any, emitModelEvent?: boolean) => {
230230
// control -> view
231231
dir.valueAccessor!.writeValue(newValue);
232232

packages/forms/src/model.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,10 +1054,10 @@ export abstract class AbstractControl {
10541054
abstract _updateValue(): void;
10551055

10561056
/** @internal */
1057-
abstract _forEachChild(cb: Function): void;
1057+
abstract _forEachChild(cb: (c: AbstractControl) => void): void;
10581058

10591059
/** @internal */
1060-
abstract _anyControls(condition: Function): boolean;
1060+
abstract _anyControls(condition: (c: AbstractControl) => boolean): boolean;
10611061

10621062
/** @internal */
10631063
abstract _allControlsDisabled(): boolean;
@@ -1099,7 +1099,7 @@ export abstract class AbstractControl {
10991099
}
11001100

11011101
/** @internal */
1102-
_onDisabledChange: Function[] = [];
1102+
_onDisabledChange: Array<(isDisabled: boolean) => void> = [];
11031103

11041104
/** @internal */
11051105
_isBoxedValue(formState: any): boolean {
@@ -1229,13 +1229,12 @@ export abstract class AbstractControl {
12291229
*/
12301230
export class FormControl extends AbstractControl {
12311231
/** @internal */
1232-
_onChange: Function[] = [];
1233-
1232+
_onChange: Array<Function> = [];
12341233
/** @internal */
1235-
_pendingValue: any;
1234+
_pendingValue: boolean = false;
12361235

12371236
/** @internal */
1238-
_pendingChange: any;
1237+
_pendingChange: boolean = false;
12391238

12401239
/**
12411240
* Creates a new `FormControl` instance.
@@ -1358,7 +1357,7 @@ export class FormControl extends AbstractControl {
13581357
/**
13591358
* @internal
13601359
*/
1361-
override _anyControls(condition: Function): boolean {
1360+
override _anyControls(condition: (c: AbstractControl) => boolean): boolean {
13621361
return false;
13631362
}
13641363

@@ -1382,7 +1381,7 @@ export class FormControl extends AbstractControl {
13821381
* Internal function to unregister a change events listener.
13831382
* @internal
13841383
*/
1385-
_unregisterOnChange(fn: Function): void {
1384+
_unregisterOnChange(fn: (value?: any, emitModelEvent?: boolean) => void): void {
13861385
removeListItem(this._onChange, fn);
13871386
}
13881387

@@ -1406,7 +1405,7 @@ export class FormControl extends AbstractControl {
14061405
/**
14071406
* @internal
14081407
*/
1409-
override _forEachChild(cb: Function): void {}
1408+
override _forEachChild(cb: (c: AbstractControl) => void): void {}
14101409

14111410
/** @internal */
14121411
override _syncPendingControls(): boolean {
@@ -1847,7 +1846,7 @@ export class FormGroup extends AbstractControl {
18471846
}
18481847

18491848
/** @internal */
1850-
override _anyControls(condition: Function): boolean {
1849+
override _anyControls(condition: (c: AbstractControl) => boolean): boolean {
18511850
for (const controlName of Object.keys(this.controls)) {
18521851
const control = this.controls[controlName];
18531852
if (this.contains(controlName) && condition(control)) {
@@ -1869,7 +1868,7 @@ export class FormGroup extends AbstractControl {
18691868
}
18701869

18711870
/** @internal */
1872-
_reduceChildren(initValue: any, fn: Function) {
1871+
_reduceChildren<T>(initValue: T, fn: (acc: T, control: AbstractControl, name: string) => T): T {
18731872
let res = initValue;
18741873
this._forEachChild((control: AbstractControl, name: string) => {
18751874
res = fn(res, control, name);
@@ -2308,7 +2307,7 @@ export class FormArray extends AbstractControl {
23082307
}
23092308

23102309
/** @internal */
2311-
override _forEachChild(cb: Function): void {
2310+
override _forEachChild(cb: (c: AbstractControl, index: number) => void): void {
23122311
this.controls.forEach((control: AbstractControl, index: number) => {
23132312
cb(control, index);
23142313
});
@@ -2322,7 +2321,7 @@ export class FormArray extends AbstractControl {
23222321
}
23232322

23242323
/** @internal */
2325-
override _anyControls(condition: Function): boolean {
2324+
override _anyControls(condition: (c: AbstractControl) => boolean): boolean {
23262325
return this.controls.some((control: AbstractControl) => control.enabled && condition(control));
23272326
}
23282327

0 commit comments

Comments
 (0)