In my Angular 2 application, I am encountering a problem when trying to add new items to an array. The issue appears to be related to Typescript types, although I'm not entirely certain. Here is the current structure of the array I am attempting to modify:
locations = [
{ city: 'Los Angelas', postalCode: '90001', coordinates: 2321 },
{ city: 'New York', postalCode: '10001', coordinates: 3432 },
];
Below is the function used for adding new postal codes to the array:
addZipcode(event) {
this.locations.push({ postalCode: this.newPostalCode });
this.newPostalCode = '';
this.addZipInput = false;
event.preventDefault();
}
The error message related to this function states:
Argument of type '{ postalCode: any; }' is not assignable to parameter of type '{ city: string; postalCode: string; coordinates: number; }'. Property 'city' is missing in type '{ postalCode: any; }'.
I need to address this issue as users will only be inputting postal codes and not cities or coordinates. While I thought I could push any object to the array, it seems that Typescript is restricting this ability.