I came across an issue I don't understand when using arrays of a custom type in typescript. I always assumed that you are only able to push a new object of the custom type into the array. However, pushing an object with additional fields seems possible if I define it beforehand.
Suppose I define a custom type:
type MyType = {
fieldOne: string,
fieldTwo: number,
};
These cases throw, IMO correct, TS errors (2322), since fieldThree is not defined in my custom type:
const temp: MyType = { fieldOne: 'foo', fieldTwo: 2, fieldThree: 'whee'};
const tempArray: MyType[] = [{fieldOne: 'bar', fieldTwo: 2, fieldThree: 'whee'}];
const newArray: MyType[] = [];
newArray.push({fieldOne: 'bar', fieldTwo: 2, fieldThree: 'whee'});
Type '{ fieldOne: string; fieldTwo: number; fieldThree: string; }' is not assignable to type 'MyType'. Object literal may only specify known properties, and 'fieldThree' does not exist in type 'MyType'.(2322)
However, the following case with a generic, untyped object does not throw an error and allows me to push data into the array:
const newArray: MyType[] = [];
const data = {fieldOne: 'bar', fieldTwo: 2, fieldThree: 'whee'};
newArray.push(data);
Am I wrong in assuming that the last case should also throw an error? What am I missing here?
Thanks!