I broke it down to these two items MapComponent and a GeoPoint class. What am I missing here?
MapComponent
// Initiate
const response: Array<LatLngExpression> = []; // Seems to work
const response: [LatLngExpression] = []; // Msg: Type any[] is not assignable to type [any]
// Add a value to the array
response.push(geoPoint.getLatLng());
GeoPoint
export clas GeoPoint {
private _id: string;
private _value: LatLngExpression; // This is a leaflet.LatLngExpression
private _selected: boolean = false;
constructor (id: string, latLng:LatLngExpression, selected?:boolean) {
this._id = id;
this._value = latLng;
if(selected){
this._selected = selected;
}
}
set()... // you get the idea
get()... // you get the idea
}
Your "Seems to work" one is correct:
const response: Array<LatLngExpression> = [];
You can also write it like this:
const response: LatLngExpression[] = [];
LatLngExpression[]
is the type of response
.
Ths is covered in the Handbook in the Basic Types > Array section.
In many cases you could also leave off the type entirely and allow TypeScript to infer it. But if you're starting with an empty array, that's actually more awkward than just providing the type annotation because you have to provide a type assertion instead. So it's probably not what you want, but just for completeness:
// I probably wouldn't do it this way
const response = [] as LatLngExpression[];
@Elijah - I don't know if the TypeScript team have a preference; there's none expressed in the linked documentation unless you want to infer one from the fact they show the
LatLngExpression[]
style first. Personally, I prefer theLatLngExpression[]
style not least because it's similar to other languages (Java, C#), but I'm not a TypeScript guru (at all). :-)