feat(forms): formControlName also accepts a number (#30606)

This commit relaxes the type of the `formControlName` input to accept both a `string` and a `number`.

Currently, when using a `FormArray`, most templates look like:

```
<div formArrayName="tags">
  <div *ngFor="let tag of tagsArray.controls; index as i">
    <input [formControlName]="i">
  </div>
</div>
```

Here `formControlName` receives a number whereas its input type is a string.

This is fine for VE and `fullTemplateTypeCheck`, but not for Ivy which does a more thorough type checking on inputs with `fullTemplateTypeCheck` enabled and throws `Type 'number' is not assignable to type 'string'`. It is fixable by using `formControlName="{{i}}"` but you have to know the difference between `a="{{b}}"` and `[a]="b"` and change it all over the application codebase. This commit allows the existing code to still type-check.

PR Close #30606
This commit is contained in:
cexbrayat
2019-05-22 10:22:13 +02:00
committed by Kara Erickson
parent e4d5102b17
commit 628b0c1154
4 changed files with 13 additions and 7 deletions

View File

@ -255,7 +255,7 @@ export declare class FormControlName extends NgControl implements OnChanges, OnD
readonly formDirective: any;
isDisabled: boolean;
/** @deprecated */ model: any;
name: string;
name: string | number | null;
readonly path: string[];
/** @deprecated */ update: EventEmitter<any>;
readonly validator: ValidatorFn | null;
@ -353,7 +353,7 @@ export declare const NG_VALUE_ACCESSOR: InjectionToken<ControlValueAccessor>;
export declare abstract class NgControl extends AbstractControlDirective {
readonly asyncValidator: AsyncValidatorFn | null;
name: string | null;
name: string | number | null;
readonly validator: ValidatorFn | null;
valueAccessor: ControlValueAccessor | null;
abstract viewToModelUpdate(newValue: any): void;