Skip to content

Commit

Permalink
fix(forms): Update types for TypeScript nullability support
Browse files Browse the repository at this point in the history
This reverts commit 6d930d2.
  • Loading branch information
mhevery authored and tbosch committed Apr 18, 2017
1 parent bc43188 commit 57bc245
Show file tree
Hide file tree
Showing 27 changed files with 343 additions and 330 deletions.
34 changes: 19 additions & 15 deletions packages/forms/src/directives/abstract_control_directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,49 @@ import {ValidationErrors} from './validators';
* @stable
*/
export abstract class AbstractControlDirective {
get control(): AbstractControl { throw new Error('unimplemented'); }
abstract get control(): AbstractControl|null;

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

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

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

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

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

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

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

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

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

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

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

get statusChanges(): Observable<any> { return this.control ? this.control.statusChanges : null; }
get statusChanges(): Observable<any>|null {
return this.control ? this.control.statusChanges : null;
}

get valueChanges(): Observable<any> { return this.control ? this.control.valueChanges : null; }
get valueChanges(): Observable<any>|null {
return this.control ? this.control.valueChanges : null;
}

get path(): string[] { return null; }
get path(): string[]|null { return null; }

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

hasError(errorCode: string, path: string[] = null): boolean {
hasError(errorCode: string, path?: string[]): boolean {
return this.control ? this.control.hasError(errorCode, path) : false;
}

getError(errorCode: string, path: string[] = null): any {
getError(errorCode: string, path?: string[]): any {
return this.control ? this.control.getError(errorCode, path) : null;
}
}
12 changes: 7 additions & 5 deletions packages/forms/src/directives/abstract_form_group_directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class AbstractFormGroupDirective extends ControlContainer implements OnIn

ngOnInit(): void {
this._checkParentType();
this.formDirective.addFormGroup(this);
this.formDirective !.addFormGroup(this);
}

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

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

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

get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
get asyncValidator(): AsyncValidatorFn|null {
return composeAsyncValidators(this._asyncValidators);
}

/** @internal */
_checkParentType(): void {}
Expand Down
6 changes: 3 additions & 3 deletions packages/forms/src/directives/control_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import {Form} from './form_interface';
*
* @stable
*/
export class ControlContainer extends AbstractControlDirective {
export abstract class ControlContainer extends AbstractControlDirective {
name: string;

/**
* Get the form to which this container belongs.
*/
get formDirective(): Form { return null; }
get formDirective(): Form|null { return null; }

/**
* Get the path to this container.
*/
get path(): string[] { return null; }
get path(): string[]|null { return null; }
}
10 changes: 5 additions & 5 deletions packages/forms/src/directives/ng_control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ function unimplemented(): any {
*/
export abstract class NgControl extends AbstractControlDirective {
/** @internal */
_parent: ControlContainer = null;
name: string = null;
valueAccessor: ControlValueAccessor = null;
_parent: ControlContainer|null = null;
name: string|null = null;
valueAccessor: ControlValueAccessor|null = null;
/** @internal */
_rawValidators: Array<Validator|ValidatorFn> = [];
/** @internal */
_rawAsyncValidators: Array<AsyncValidator|AsyncValidatorFn> = [];

get validator(): ValidatorFn { return <ValidatorFn>unimplemented(); }
get asyncValidator(): AsyncValidatorFn { return <AsyncValidatorFn>unimplemented(); }
get validator(): ValidatorFn|null { return <ValidatorFn>unimplemented(); }
get asyncValidator(): AsyncValidatorFn|null { return <AsyncValidatorFn>unimplemented(); }

abstract viewToModelUpdate(newValue: any): void;
}
2 changes: 1 addition & 1 deletion packages/forms/src/directives/ng_form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class NgForm extends ControlContainer implements Form {

updateModel(dir: NgControl, value: any): void {
resolvedPromise.then(() => {
const ctrl = <FormControl>this.form.get(dir.path);
const ctrl = <FormControl>this.form.get(dir.path !);
ctrl.setValue(value);
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/forms/src/directives/ng_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ export class NgModel extends NgControl implements OnChanges,

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

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

get asyncValidator(): AsyncValidatorFn {
get asyncValidator(): AsyncValidatorFn|null {
return composeAsyncValidators(this._rawAsyncValidators);
}

Expand All @@ -176,7 +176,7 @@ export class NgModel extends NgControl implements OnChanges,
}

private _isStandalone(): boolean {
return !this._parent || (this.options && this.options.standalone);
return !this._parent || !!(this.options && this.options.standalone);
}

private _setUpStandalone(): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/directives/number_value_accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
}

registerOnChange(fn: (_: number) => void): void {
registerOnChange(fn: (_: number|null) => void): void {
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
}
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
Expand Down
2 changes: 1 addition & 1 deletion packages/forms/src/directives/range_value_accessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class RangeValueAccessor implements ControlValueAccessor {
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', parseFloat(value));
}

registerOnChange(fn: (_: number) => void): void {
registerOnChange(fn: (_: number|null) => void): void {
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export class FormControlDirective extends NgControl implements OnChanges {
ngOnChanges(changes: SimpleChanges): void {
if (this._isControlChanged(changes)) {
setUpControl(this.form, this);
if (this.control.disabled && this.valueAccessor.setDisabledState) {
this.valueAccessor.setDisabledState(true);
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
this.valueAccessor !.setDisabledState !(true);
}
this.form.updateValueAndValidity({emitEvent: false});
}
Expand All @@ -101,9 +101,9 @@ export class FormControlDirective extends NgControl implements OnChanges {

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

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

get asyncValidator(): AsyncValidatorFn {
get asyncValidator(): AsyncValidatorFn|null {
return composeAsyncValidators(this._rawAsyncValidators);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
this.update.emit(newValue);
}

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

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

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

get asyncValidator(): AsyncValidatorFn {
return composeAsyncValidators(this._rawAsyncValidators);
return composeAsyncValidators(this._rawAsyncValidators) !;
}

get control(): FormControl { return this._control; }
Expand All @@ -151,8 +151,8 @@ export class FormControlName extends NgControl implements OnChanges, OnDestroy {
private _setUpControl() {
this._checkParentType();
this._control = this.formDirective.addControl(this);
if (this.control.disabled && this.valueAccessor.setDisabledState) {
this.valueAccessor.setDisabledState(true);
if (this.control.disabled && this.valueAccessor !.setDisabledState) {
this.valueAccessor !.setDisabledState !(true);
}
this._added = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class FormGroupDirective extends ControlContainer implements Form,
private _oldForm: FormGroup;
directives: FormControlName[] = [];

@Input('formGroup') form: FormGroup = null;
@Input('formGroup') form: FormGroup = null !;
@Output() ngSubmit = new EventEmitter();

constructor(
Expand Down Expand Up @@ -167,10 +167,10 @@ export class FormGroupDirective extends ControlContainer implements Form,

private _updateValidators() {
const sync = composeValidators(this._validators);
this.form.validator = Validators.compose([this.form.validator, sync]);
this.form.validator = Validators.compose([this.form.validator !, sync !]);

const async = composeAsyncValidators(this._asyncValidators);
this.form.asyncValidator = Validators.composeAsync([this.form.asyncValidator, async]);
this.form.asyncValidator = Validators.composeAsync([this.form.asyncValidator !, async !]);
}

private _checkFormPresent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy

ngOnInit(): void {
this._checkParentType();
this.formDirective.addFormArray(this);
this.formDirective !.addFormArray(this);
}

ngOnDestroy(): void {
Expand All @@ -175,17 +175,19 @@ export class FormArrayName extends ControlContainer implements OnInit, OnDestroy
}
}

get control(): FormArray { return this.formDirective.getFormArray(this); }
get control(): FormArray { return this.formDirective !.getFormArray(this); }

get formDirective(): FormGroupDirective {
get formDirective(): FormGroupDirective|null {
return this._parent ? <FormGroupDirective>this._parent.formDirective : null;
}

get path(): string[] { return controlPath(this.name, this._parent); }

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

get asyncValidator(): AsyncValidatorFn { return composeAsyncValidators(this._asyncValidators); }
get asyncValidator(): AsyncValidatorFn|null {
return composeAsyncValidators(this._asyncValidators);
}

private _checkParentType(): void {
if (_hasInvalidParent(this._parent)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const SELECT_VALUE_ACCESSOR: Provider = {
multi: true
};

function _buildValueString(id: string, value: any): string {
function _buildValueString(id: string | null, value: any): string {
if (id == null) return `${value}`;
if (value && typeof value === 'object') value = 'Object';
return `${id}: ${value}`.slice(0, 50);
Expand Down Expand Up @@ -118,7 +118,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {

writeValue(value: any): void {
this.value = value;
const id: string = this._getOptionId(value);
const id: string|null = this._getOptionId(value);
if (id == null) {
this._renderer.setElementProperty(this._elementRef.nativeElement, 'selectedIndex', -1);
}
Expand All @@ -142,7 +142,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
_registerOption(): string { return (this._idCounter++).toString(); }

/** @internal */
_getOptionId(value: any): string {
_getOptionId(value: any): string|null {
for (const id of Array.from(this._optionMap.keys())) {
if (this._compareWith(this._optionMap.get(id), value)) return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,17 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
}

/** @internal */
_getOptionId(value: any): string {
_getOptionId(value: any): string|null {
for (const id of Array.from(this._optionMap.keys())) {
if (this._compareWith(this._optionMap.get(id)._value, value)) return id;
if (this._compareWith(this._optionMap.get(id) !._value, value)) return id;
}
return null;
}

/** @internal */
_getOptionValue(valueString: string): any {
const id: string = _extractId(valueString);
return this._optionMap.has(id) ? this._optionMap.get(id)._value : valueString;
return this._optionMap.has(id) ? this._optionMap.get(id) !._value : valueString;
}
}

Expand Down
Loading

0 comments on commit 57bc245

Please sign in to comment.