Javascript 为什么推送显示“any[]”类型的参数不可分配给“从不”错误类型的参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52054140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:56:02  来源:igfitidea点击:

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

javascripttypescriptleaflet

提问by Parsaria

In this code i get the fallowing error:

在这段代码中,我得到了以下错误:

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

“any[]”类型的参数不可分配给“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);
});

回答by Matt McCutchen

With var markers: []you are declaring the markersarray 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.

随着var markers: []您将markers数组声明为具有永久空数组的类型。您可能打算var markers = []将其初始化为空但允许添加项目。

回答by Denis TRUFFAUT

Change this :

改变这一点:

const a = [];

By this :

这样 :

const a = Array();

回答by Programmer

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.

never 类型是每个类型的子类型,并且可以分配给每个类型;但是,没有任何类型是 never 的子类型或可分配给 never(除了 never 本身)。甚至 any 也不能分配给 never。

From the TypeScript docs

来自TypeScript 文档