refactor(forms): use a type guard to get rid of casts (#32541)
Use an explicit type guard when checking if a given object is of type AbstractControlOptions, instead of a simple function returning a boolean value. This allows us to remove manual type casting when using this function, relying instead on TypeScript to infer correct types. PR Close #32541
This commit is contained in:
parent
382aa50c54
commit
06a8ad28a7
|
@ -71,11 +71,7 @@ function _find(control: AbstractControl, path: Array<string|number>|string, deli
|
||||||
|
|
||||||
function coerceToValidator(validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|
|
function coerceToValidator(validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|
|
||||||
null): ValidatorFn|null {
|
null): ValidatorFn|null {
|
||||||
const validator =
|
const validator = isOptionsObj(validatorOrOpts) ? validatorOrOpts.validators : validatorOrOpts;
|
||||||
(isOptionsObj(validatorOrOpts) ? (validatorOrOpts as AbstractControlOptions).validators :
|
|
||||||
validatorOrOpts) as ValidatorFn |
|
|
||||||
ValidatorFn[] | null;
|
|
||||||
|
|
||||||
return Array.isArray(validator) ? composeValidators(validator) : validator || null;
|
return Array.isArray(validator) ? composeValidators(validator) : validator || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,10 +80,7 @@ function coerceToAsyncValidator(
|
||||||
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null): AsyncValidatorFn|
|
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null): AsyncValidatorFn|
|
||||||
null {
|
null {
|
||||||
const origAsyncValidator =
|
const origAsyncValidator =
|
||||||
(isOptionsObj(validatorOrOpts) ? (validatorOrOpts as AbstractControlOptions).asyncValidators :
|
isOptionsObj(validatorOrOpts) ? validatorOrOpts.asyncValidators : asyncValidator;
|
||||||
asyncValidator) as AsyncValidatorFn |
|
|
||||||
AsyncValidatorFn | null;
|
|
||||||
|
|
||||||
return Array.isArray(origAsyncValidator) ? composeAsyncValidators(origAsyncValidator) :
|
return Array.isArray(origAsyncValidator) ? composeAsyncValidators(origAsyncValidator) :
|
||||||
origAsyncValidator || null;
|
origAsyncValidator || null;
|
||||||
}
|
}
|
||||||
|
@ -119,7 +112,7 @@ export interface AbstractControlOptions {
|
||||||
|
|
||||||
|
|
||||||
function isOptionsObj(validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|
|
function isOptionsObj(validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|
|
||||||
null): boolean {
|
null): validatorOrOpts is AbstractControlOptions {
|
||||||
return validatorOrOpts != null && !Array.isArray(validatorOrOpts) &&
|
return validatorOrOpts != null && !Array.isArray(validatorOrOpts) &&
|
||||||
typeof validatorOrOpts === 'object';
|
typeof validatorOrOpts === 'object';
|
||||||
}
|
}
|
||||||
|
@ -914,8 +907,8 @@ export abstract class AbstractControl {
|
||||||
|
|
||||||
/** @internal */
|
/** @internal */
|
||||||
_setUpdateStrategy(opts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null): void {
|
_setUpdateStrategy(opts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null): void {
|
||||||
if (isOptionsObj(opts) && (opts as AbstractControlOptions).updateOn != null) {
|
if (isOptionsObj(opts) && opts.updateOn != null) {
|
||||||
this._updateOn = (opts as AbstractControlOptions).updateOn!;
|
this._updateOn = opts.updateOn!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue