X 类型的参数不可分配给 Y 类型的参数。对象字面量只能指定已知属性,并且 X 不存在于类型 Y 中

2024-03-11

我正在尝试让 Leaflet 和插件在 Ionic 2 项目中协同工作。我已经安装并导入了 Leaflet 本身以及传单路径变换 https://github.com/w8r/Leaflet.Path.Transform插件,以及Leaflet类型声明 https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/leaflet-editable包含在 DefinatelyTyped 中。如果我添加,代码就可以正常工作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();

draggable and transform不是基本传单中的有效选项(也不是.transform),所以我写了一个类型声明文件PROJECT_DIR\node_modules\@types\leaflet-path-transform\index.d.ts来定义它们。到目前为止我所写的内容是基于leaflet-editable 的类型声明 https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/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'.

有人可以帮我弄清楚我在这里做错了什么吗?起初我以为新的类型声明文件只是被忽略了,但如果我在其中留下拼写错误,它会触发新的错误,所以它似乎确实有效果。

如果有帮助的话,这是我工作的环境:

  • 离子框架:2.0.0
  • 离子本机:2.4.1
  • 离子应用程序脚本:1.0.0
  • 角度核心:2.2.1
  • Angular 编译器 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/ http://yunkus.com/angular-error-object-literal-may-only-specify-known-properties/解决起来超级容易,但很难找到原因。使用“any”是一种方法,但这不是最好的解决方案。因为我们只想使用自定义类来创建变量,对吧?幸运的是,答案已经在那篇文章中了。你可以阅读它,但是这篇文章是中国人写的。希望你能得到它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

X 类型的参数不可分配给 Y 类型的参数。对象字面量只能指定已知属性,并且 X 不存在于类型 Y 中 的相关文章

随机推荐