I recently started delving into Typescript and encountered a peculiar issue.
An interface was declared as follows:
Location.d.ts
interface Location {
lat: number;
lng: number;
}
This interface is utilized in the following manner:
reducer.tsx
interface MapState extends State {
locations: Location[];
}
Now, when attempting to define a variable with the type MapState
, an error arises:
Type ... cannot be assigned to type 'MapState'. Properties of 'locations' are incompatible. Type ... cannot be assigned to type 'Location[]'. Type ... cannot be assigned to type 'Location'. Property 'hash' is missing in type ...
My declaration looks like this:
const initialState: MapState = {
locations: [
{ lat: 33.488069, lng: -112.072972 }
]
};
If I explicitly cast the Location
, it resolves the issue:
const initialState: MapState = {
locations: [
({ lat: 33.488069, lng: -112.072972 } as Location)
]
};
However, this method seems excessively verbose. Is there something I am overlooking?