I'm working in a Typescript Vue project with Leaflet. I've found some code to lazy-load map markers, but it was written in Javascript. It works fine, but I get VSCode errors and compiler warnings because this is not typed. How do I type this?
Update 1: I got the first type to resolve using @aplet123 suggestion below. But I can't figure out the second one on this._updateIconVisibility. I think this is because I'm actually using the function to extend the existing functionality (i.e. _updateIconVisibility does not actually exist on a type). So now what do I do? I suspect it is best practice to just create a custom class with the methods I need, but I don't know if it is actually more common to provide maybe an anonymous object or something else...
L.Marker.addInitHook(function (this: L.Marker) {
this.on(
'add',
function () {
this._updateIconVisibility = function () {
var map = this._map,
isVisible = map.getBounds().contains(this.getLatLng()),
wasVisible = this._wasVisible,
icon = this._icon,
iconParent = this._iconParent,
shadow = this._shadow,
shadowParent = this._shadowParent
// remember parent of icon
if (!iconParent) {
iconParent = this._iconParent = icon.parentNode
}
if (shadow && !shadowParent) {
shadowParent = this._shadowParent = shadow.parentNode
}
// add/remove from DOM on change
if (isVisible != wasVisible) {
if (isVisible) {
iconParent.appendChild(icon)
if (shadow) {
shadowParent.appendChild(shadow)
}
} else {
iconParent.removeChild(icon)
if (shadow) {
shadowParent.removeChild(shadow)
}
}
this._wasVisible = isVisible
}
}
// on map size change, remove/add icon from/to DOM
this._map.on(
'resize moveend zoomend',
this._updateIconVisibility,
this
)
this._updateIconVisibility()
},
this
)
})

this(it's not an actual argument and won't be compiled into the resulting javascript, it's just to notate thethistype).strict: truethisargument and they will be in the correct order?strict: truein my .tsconfig