Within my component, I have defined a property array as follows:
array: number[] | string[] = ['1', '2'];
In the template, I am using ngFor to iterate over the elements of this array:
<div *ngFor="let element of array">
{{element}}
</div>
However, the compiler is throwing an error:
error TS2322:
Type 'number[] | string[]' is not assignable to
type '(number[] & NgIterable<number>) | null | undefined'.
I am puzzled by Angular inferring that the type of the array
property should be
number[] & NgIterable<number>
in this scenario.
I am aware that I can resolve the error by using $any()
within the template or by setting
angularCompilerOptions.strictTemplates
to false
in tsconfig.json
, but I prefer to avoid these solutions unless necessary.
I am hesitant to change the type of the array
property to
(number | string)[]</code as it would not accurately reflect my intention of having <code>array
contain only numbers or only strings.