[英]Compile time error on Layers, but runtime okay
Code below works fine at runtime, but receives an error at compile time which prevents me from building production code unless I ignore errors, which I don't want to do.下面的代码在运行时工作正常,但在编译时收到一个错误,这阻止我构建生产代码,除非我忽略错误,我不想这样做。
I'm unable to cast to a marker object as marker appears to be a method.我无法转换为标记对象,因为标记似乎是一种方法。
HTML HTML
<div class="map-frame" leaflet [leafletOptions]="options"
[leafletLayers]="markers" [leafletLayersControl]="layersControl"
[(leafletCenter)]="center" (leafletMapReady)="onMapReady($event)"
(leafletCenterChange)="onCenterChange($event)"
(leafletMouseMove)="onMouseMove($event)"></div>
TypeScript打字稿
markers: Layer[] = [];
var markerObj: MarkerModel = {};
markerObj.guid = this.utils.uuidv4();
markerObj.iconUrl = pItem;
markerObj.latitude = this.lat;
markerObj.longitude = this.lng;
const newMarker = marker(
[markerObj.latitude, markerObj.longitude],
{
icon: icon( {
iconSize: [38, 38],
iconAnchor: [13, 13],
iconUrl: pItem
} ),
title: markerObj.guid
}
).on( 'click', () => {
this.zone.run(() => {
this.onMarkerClick( markerObj );
} );
} );
this.markers.push( newMarker );
for ( var i = this.markers.length - 1; i >= 0; i-- ) {
console.log( i, this.markers[i].title ); //compile time error
if ( this.markers[i].title == pGuid ) { //compile time error
this.markers.splice( i, 1 );
//todo update server
break;
}
}
ERROR in src/app/map/map.component.ts:265:49 - error TS2339: Property 'title' does not exist on type 'Layer'. src/app/map/map.component.ts:265:49 中的错误 - 错误 TS2339:“图层”类型上不存在属性“标题”。
So, Layer
does not have a property title
, but MarkerOptions
does.因此,
Layer
没有属性title
,但MarkerOptions
有。 The way the typings were set up for Leaflet, it looks like they'd want you to access it via marker.options.title.
为 Leaflet 设置类型的方式,看起来他们希望你通过
marker.options.title.
访问它marker.options.title.
But, I'm not sure if that'll work the way you expect.但是,我不确定这是否会按您期望的方式工作。
The typings definition (see the link below) is what determines what is and is not valid at compile type when it'd doing type checking.类型定义(参见下面的链接)决定了编译类型在进行类型检查时什么是有效的,什么是无效的。
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/leaflet/index.d.ts https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/leaflet/index.d.ts
You have a couple options.你有几个选择。 First, you can try to find a way to access the
title
property in a way that is valid from a type checking perspective.首先,您可以尝试找到一种从类型检查的角度来看有效的方式来访问
title
属性。 Second, you could just cast to any
to get it to compile without error/warning.其次,您可以将其强制转换为
any
以使其在没有错误/警告的情况下进行编译。 The former is better, but the latter is fine if you just want to get it to work.前者更好,但如果您只是想让它工作,后者也可以。
Here's an example of casting to any
:这是转换为
any
的示例:
if ( (this.markers[i] as any).title == pGuid ) { //compile time error
this.markers.splice( i, 1 );
//todo update server
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.