Skip to content

Commit 57bc245

Browse files
mheverytbosch
authored andcommitted
fix(forms): Update types for TypeScript nullability support
This reverts commit 6d930d2.
1 parent bc43188 commit 57bc245

27 files changed

+343
-330
lines changed

packages/forms/src/directives/abstract_control_directive.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,49 @@ import {ValidationErrors} from './validators';
1818
* @stable
1919
*/
2020
export abstract class AbstractControlDirective {
21-
get control(): AbstractControl { throw new Error('unimplemented'); }
21+
abstract get control(): AbstractControl|null;
2222

2323
get value(): any { return this.control ? this.control.value : null; }
2424

25-
get valid(): boolean { return this.control ? this.control.valid : null; }
25+
get valid(): boolean|null { return this.control ? this.control.valid : null; }
2626

27-
get invalid(): boolean { return this.control ? this.control.invalid : null; }
27+
get invalid(): boolean|null { return this.control ? this.control.invalid : null; }
2828

29-
get pending(): boolean { return this.control ? this.control.pending : null; }
29+
get pending(): boolean|null { return this.control ? this.control.pending : null; }
3030

3131
get errors(): ValidationErrors|null { return this.control ? this.control.errors : null; }
3232

33-
get pristine(): boolean { return this.control ? this.control.pristine : null; }
33+
get pristine(): boolean|null { return this.control ? this.control.pristine : null; }
3434

35-
get dirty(): boolean { return this.control ? this.control.dirty : null; }
35+
get dirty(): boolean|null { return this.control ? this.control.dirty : null; }
3636

37-
get touched(): boolean { return this.control ? this.control.touched : null; }
37+
get touched(): boolean|null { return this.control ? this.control.touched : null; }
3838

39-
get untouched(): boolean { return this.control ? this.control.untouched : null; }
39+
get untouched(): boolean|null { return this.control ? this.control.untouched : null; }
4040

41-
get disabled(): boolean { return this.control ? this.control.disabled : null; }
41+
get disabled(): boolean|null { return this.control ? this.control.disabled : null; }
4242

43-
get enabled(): boolean { return this.control ? this.control.enabled : null; }
43+
get enabled(): boolean|null { return this.control ? this.control.enabled : null; }
4444

45-
get statusChanges(): Observable<any> { return this.control ? this.control.statusChanges : null; }
45+
get statusChanges(): Observable<any>|null {
46+
return this.control ? this.control.statusChanges : null;
47+
}
4648

47-
get valueChanges(): Observable<any> { return this.control ? this.control.valueChanges : null; }
49+
get valueChanges(): Observable<any>|null {
50+
return this.control ? this.control.valueChanges : null;
51+
}
4852

49-
get path(): string[] { return null; }
53+
get path(): string[]|null { return null; }
5054

5155
reset(value: any = undefined): void {
5256
if (this.control) this.control.reset(value);
5357
}
5458

55-
hasError(errorCode: string, path: string[] = null): boolean {
59+
hasError(errorCode: string, path?: string[]): boolean {
5660
return this.control ? this.control.hasError(errorCode, path) : false;
5761
}
5862

59-
getError(errorCode: string, path: string[] = null): any {
63+
getError(errorCode: string, path?: string[]): any {
6064
return this.control ? this.control.getError(errorCode, path) : null;
6165
}
6266
}

packages/forms/src/directives/abstract_form_group_directive.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
3434

3535
ngOnInit(): void {
3636
this._checkParentType();
37-
this.formDirective.addFormGroup(this);
37+
this.formDirective !.addFormGroup(this);
3838
}
3939

4040
ngOnDestroy(): void {
@@ -46,7 +46,7 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
4646
/**
4747
* Get the {@link FormGroup} backing this binding.
4848
*/
49-
get control(): FormGroup { return this.formDirective.getFormGroup(this); }
49+
get control(): FormGroup { return this.formDirective !.getFormGroup(this); }
5050

5151
/**
5252
* Get the path to this control group.
@@ -56,11 +56,13 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn
5656
/**
5757
* Get the {@link Form} to which this group belongs.
5858
*/
59-
get formDirective(): Form { return this._parent ? this._parent.formDirective : null; }
59+
get formDirective(): Form|null { return this._parent ? this._parent.formDirective : null; }
6060

61-
get validator(): ValidatorFn { return composeValidators(this._validators); }
61+
get validator(): ValidatorFn|null { return composeValidators(this._validators); }
6262

63-
get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
63+
get asyncValidator(): AsyncValidatorFn|null {
64+
return composeAsyncValidators(this._asyncValidators);
65+
}
6466

6567
/** @internal */
6668
_checkParentType(): void {}

packages/forms/src/directives/control_container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ import {Form} from './form_interface';
1717
*
1818
* @stable
1919
*/
20-
export class ControlContainer extends AbstractControlDirective {
20+
export abstract class ControlContainer extends AbstractControlDirective {
2121
name: string;
2222

2323
/**
2424
* Get the form to which this container belongs.
2525
*/
26-
get formDirective(): Form { return null; }
26+
get formDirective(): Form|null { return null; }
2727

2828
/**
2929
* Get the path to this container.
3030
*/
31-
get path(): string[] { return null; }
31+
get path(): string[]|null { return null; }
3232
}

packages/forms/src/directives/ng_control.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ function unimplemented(): any {
2626
*/
2727
export abstract class NgControl extends AbstractControlDirective {
2828
/** @internal */
29-
_parent: ControlContainer = null;
30-
name: string = null;
31-
valueAccessor: ControlValueAccessor = null;
29+
_parent: ControlContainer|null = null;
30+
name: string|null = null;
31+
valueAccessor: ControlValueAccessor|null = null;
3232
/** @internal */
3333
_rawValidators: Array<Validator|ValidatorFn> = [];
3434
/** @internal */
3535
_rawAsyncValidators: Array<AsyncValidator|AsyncValidatorFn> = [];
3636

37-
get validator(): ValidatorFn { return <ValidatorFn>unimplemented(); }
38-
get asyncValidator(): AsyncValidatorFn { return <AsyncValidatorFn>unimplemented(); }
37+
get validator(): ValidatorFn|null { return <ValidatorFn>unimplemented(); }
38+
get asyncValidator(): AsyncValidatorFn|null { return <AsyncValidatorFn>unimplemented(); }
3939

4040
abstract viewToModelUpdate(newValue: any): void;
4141
}

packages/forms/src/directives/ng_form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class NgForm extends ControlContainer implements Form {
130130

131131
updateModel(dir: NgControl, value: any): void {
132132
resolvedPromise.then(() => {
133-
const ctrl = <FormControl>this.form.get(dir.path);
133+
const ctrl = <FormControl>this.form.get(dir.path !);
134134
ctrl.setValue(value);
135135
});
136136
}

packages/forms/src/directives/ng_model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ export class NgModel extends NgControl implements OnChanges,
158158

159159
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
160160

161-
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
161+
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
162162

163-
get asyncValidator(): AsyncValidatorFn {
163+
get asyncValidator(): AsyncValidatorFn|null {
164164
return composeAsyncValidators(this._rawAsyncValidators);
165165
}
166166

@@ -176,7 +176,7 @@ export class NgModel extends NgControl implements OnChanges,
176176
}
177177

178178
private _isStandalone(): boolean {
179-
return !this._parent || (this.options && this.options.standalone);
179+
return !this._parent || !!(this.options && this.options.standalone);
180180
}
181181

182182
private _setUpStandalone(): void {

packages/forms/src/directives/number_value_accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
4747
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
4848
}
4949

50-
registerOnChange(fn: (_: number) => void): void {
50+
registerOnChange(fn: (_: number|null) => void): void {
5151
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
5252
}
5353
registerOnTouched(fn: () => void): void { this.onTouched = fn; }

packages/forms/src/directives/range_value_accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class RangeValueAccessor implements ControlValueAccessor {
4545
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', parseFloat(value));
4646
}
4747

48-
registerOnChange(fn: (_: number) => void): void {
48+
registerOnChange(fn: (_: number|null) => void): void {
4949
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
5050
}
5151

packages/forms/src/directives/reactive_directives/form_control_directive.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ export class FormControlDirective extends NgControl implements OnChanges {
8888
ngOnChanges(changes: SimpleChanges): void {
8989
if (this._isControlChanged(changes)) {
9090
setUpControl(this.form, this);
91-
if (this.control.disabled && this.valueAccessor.setDisabledState) {
92-
this.valueAccessor.setDisabledState(true);
91+
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
92+
this.valueAccessor !.setDisabledState !(true);
9393
}
9494
this.form.updateValueAndValidity({emitEvent: false});
9595
}
@@ -101,9 +101,9 @@ export class FormControlDirective extends NgControl implements OnChanges {
101101

102102
get path(): string[] { return []; }
103103

104-
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
104+
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
105105

106-
get asyncValidator(): AsyncValidatorFn {
106+
get asyncValidator(): AsyncValidatorFn|null {
107107
return composeAsyncValidators(this._rawAsyncValidators);
108108
}
109109

packages/forms/src/directives/reactive_directives/form_control_name.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
125125
this.update.emit(newValue);
126126
}
127127

128-
get path(): string[] { return controlPath(this.name, this._parent); }
128+
get path(): string[] { return controlPath(this.name, this._parent !); }
129129

130130
get formDirective(): any { return this._parent ? this._parent.formDirective : null; }
131131

132-
get validator(): ValidatorFn { return composeValidators(this._rawValidators); }
132+
get validator(): ValidatorFn|null { return composeValidators(this._rawValidators); }
133133

134134
get asyncValidator(): AsyncValidatorFn {
135-
return composeAsyncValidators(this._rawAsyncValidators);
135+
return composeAsyncValidators(this._rawAsyncValidators) !;
136136
}
137137

138138
get control(): FormControl { return this._control; }
@@ -151,8 +151,8 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
151151
private _setUpControl() {
152152
this._checkParentType();
153153
this._control = this.formDirective.addControl(this);
154-
if (this.control.disabled && this.valueAccessor.setDisabledState) {
155-
this.valueAccessor.setDisabledState(true);
154+
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
155+
this.valueAccessor !.setDisabledState !(true);
156156
}
157157
this._added = true;
158158
}

0 commit comments

Comments
 (0)