Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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); }); 
like image 774
Parsaria Avatar asked Aug 28 '18 09:08

Parsaria


People also ask

Is not assignable to type never []' ts 2322?

ts(2322) arr[0] = 'a'; We declared an empty array and it got assigned a type of never[] . This type represents an array that will never contain any elements (will always be empty). To solve this, we have to explicitly type the empty array.

Is not assignable to parameter of type to?

The error "Argument of type string | undefined is not assignable to parameter of type string" occurs when a possibly undefined value is passed to a function that expects a string . To solve the error, use a type guard to verify the value is a string before passing it to the function.

Is not assignable to parameter of type never useState?

The error "Type is not assignable to type 'never'" occurs when we declare an empty state array with the useState hook but don't type the array. To solve the error, use a generic to type the state array, e.g. const [arr, setArr] = useState<string[]>([]) . Here is an example of how the error occurs.

What is a never []?

TypeScript introduced a new type never , which indicates the values that will never occur. The never type is used when you are sure that something is never going to occur. For example, you write a function which will not return to its end point or always throws an exception. Example: never.


2 Answers

Change this :

const a = []; 

By this :

const a = Array(); 
like image 135
Denis TRUFFAUT Avatar answered Sep 18 '22 01:09

Denis TRUFFAUT


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.

like image 37
Matt McCutchen Avatar answered Sep 19 '22 01:09

Matt McCutchen