我正在尝试让 Leaflet 和一个插件在 Ionic 2 项目中协同工作。我已经安装并导入了 Leaflet 本身和 leaflet-path-transform插件,以及 the Leaflet type declaration包含在 DefinatelyTyped 中。如果我添加 declare var L: any;
以忽略类型声明,则所有代码都有效。
这是页面 Controller (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();
draggable
和 transform
在基本 Leaflet 中不是有效选项(.transform
也不是),所以我写了一个类型声明文件在PROJECT_DIR\node_modules\@types\leaflet-path-transform\index.d.ts
来定义它们。到目前为止我写的是基于 the type declaration for 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'.
谁能帮我弄清楚我在这里做错了什么?起初我以为新类型声明文件只是被忽略了,但如果我在其中留下错字,它就会触发新的错误,所以它似乎确实有影响。
如果有帮助,这是我工作的环境:
- ionic 框架:2.0.0
- 原生 ionic :2.4.1
- Ionic 应用脚本:1.0.0
- 角度核心:2.2.1
- 角度编译器 CLI:2.2.1
- 节点:6.9.4
- 操作系统平台:Windows 10
- 导航器平台:Win32
- 用户代理:Mozilla/5.0(Windows NT 10.0;WOW64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/56.0.2924.87 Safari/537.36
最佳答案
你可以阅读这篇文章http://yunkus.com/angular-error-object-literal-may-only-specify-known-properties/ 解决起来 super 容易,但很难找到原因。使用“任何”是一种方法,但它不是最好的解决方案。因为我们只想使用自定义类来创建变量,对吗?幸运的是,那个帖子里已经有了答案。你可以阅读它,但是帖子是中国人写的。希望你能得到它。
关于typescript - X 类型的参数不能分配给 Y 类型的参数。对象文字只能指定已知属性,并且 X 在类型 Y 中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42303181/