为什么push显示类型'any []'的参数不能分配给'never'类型的参数错误?

时间:2018-08-28 09:10:43

标签: javascript typescript leaflet

在这段代码中,我得到了休闲错误:

  

类型'any []'的参数不能分配给类型的参数   “从不”

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 个答案:

答案 0 :(得分:12)

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

答案 1 :(得分:1)

  

never类型是每种类型的子类型,并且可以分配给每种类型;但是,任何类型都不是(永远不会除外的)永不的子类型或可分配给它的子类型。甚至任何东西都无法分配给永不。

TypeScript docs

答案 2 :(得分:1)

在 tsconfig.json -

 "noImplicitReturns": false,

 "strictNullChecks":false,

enter image description here

解决方案:输入'never'

enter image description here

答案 3 :(得分:0)

Change this :

const a = [];

By this :

const a = Array();