Why push shows argument of type 'any[]' is not assignable to parameter of type 'never' error?

Viewed 88036

In this code i get the fallowing error:

Argument of type 'any[]' is not assignable to parameter of type 'never'

var markers: [];
this.Getlapoints(this.map.getCenter(), 500000).then(data => {
  for (var key in data) {
    Leaflet.marker(data[key].location, //{ icon: greenIcon            }
    ).addTo(this.map).bindPopup(data[key].caption);
    // markers.push(data[key].location.lat,data[key].location.lng);
    // markers.push(data[key].location);

    var lat = data[key].location.lat;
    var lng = data[key].location.lng;
    markers.push([lat, lng]);
  }
  console.log(markers);
});
4 Answers

Change this :

const a = [];

By this :

const a = Array();

With var markers: [] you are declaring the markers array as having the type of a permanently empty array. You probably meant var markers = [] to initialize it to empty but allow items to be added.

In tsconfig.json -

 "noImplicitReturns": false,

 "strictNullChecks":false,

enter image description here

Solution: type as 'never'

enter image description here

The never type is a subtype of, and assignable to, every type; however, no type is a subtype of, or assignable to, never (except never itself). Even any isn’t assignable to never.

From the TypeScript docs

Related