I define an array of a specific type in typescript. When assigning values that do not correspond to the type, error messages are generated as required. An assignment of the type any works, even if the type is not correct.
Do I misunderstand the type definition of an array? Or do I underestimate the "power" of the anys :-)
Here is a brief example:
export class ItemList {
items: Array<string> = [];
constructor() {
// push a string directly => works
this.items.push('item 1');
// push a string variable => works
let item2:string = 'item 2';
this.items.push(item2);
// push a number variable => doesn't work
let item3 = 3;
this.items.push(item3);
// push a number as any type => works
let item4:any = 4;
this.items.push(item4);
}
}
let itemList = new ItemList();
The error from the tsc is:
error TS2345: Argument of type 'number' is not assignable to parameter of type
'string'.
Funny thing: the plunkers here works.