![](/img/trans.png)
[英]Argument of type X is not assignable to parameter of type Y. Type X is missing the following properties from type Y
[英]Argument of type X is not assignable to parameter of type Y. Object literal may only specify known properties, and X does not exist in type Y
我試圖讓Leaflet和一個插件在Ionic 2項目中一起工作。 我已經安裝並導入Leaflet本身和leaflet-path-transform插件,以及DefinatelyTyped中包含的Leaflet類型聲明 。 如果我添加declare var L: any;
則代碼全部正常工作declare var L: any;
忽略類型聲明。
這是頁面控制器(home.ts)的重要部分:
var polyline = new L.Polyline(
L.GeoJSON.coordsToLatLngs([
[114.14314270019531, 22.49479484975443],
[114.21798706054688, 22.524608511026262],
[114.20768737792969, 22.524608511026262],
[114.20768737792969, 22.536024805886974]
]), {
weight: 15,
draggable: true,
transform: true
})
.bindPopup("L.Polyline")
.addTo(map);
polyline.transform.enable();
在基本Leaflet中, draggable
和transform
不是有效的選項(也不是.transform
),因此我在PROJECT_DIR\\node_modules\\@types\\leaflet-path-transform\\index.d.ts
編寫了一個類型聲明文件來定義它們。 到目前為止,我所寫的內容都是基於leaflet-editable的類型聲明 ,因為該插件在創建L.Map對象時允許使用新的MapOptions。
/// <reference types="leaflet" />
declare namespace L {
export interface Polyline {
draggable: boolean;
}
namespace Polyline {
export interface PolylineOptions {
draggable: boolean;
}
}
}
到目前為止,這僅解決了第一個選項,但未能成功清除TypeScript錯誤:
Typescript Error
Argument of type '{ weight: number; draggable: boolean; }' is not assignable to parameter of type 'PolylineOptions'. Object literal may only specify known properties, and 'draggable' does not exist in type 'PolylineOptions'.
有人可以幫我弄清楚我在做什么錯嗎? 起初我以為只是忽略了新的類型聲明文件,但是如果我在其中輸入錯字,則會觸發新的錯誤,因此它似乎確實起作用。
如果有幫助,這里是我正在使用的環境:
您可以閱讀這篇文章http://yunkus.com/angular-error-object-literal-may-only-specify-known-properties/超級容易解決,但很難找到原因。 使用“ any”仍然是一種方法,但是它不是最佳解決方案。因為我們只想使用自定義類來創建變量,對嗎? 幸運的是,該帖子中的答案已經刪除。您可以閱讀它,但該帖子是用中文撰寫的。希望您可以得到它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.