类型X的参数不能赋予类型Y的参数。对象文字只能指定已知属性,而类型Y

时间:2017-02-17 16:44:49

标签: typescript leaflet typescript-typings

我正试图让Leaflet和一个插件在Ionic 2项目中一起工作。我已经安装并导入了Leaflet本身和leaflet-path-transform插件,以及DefinatelyTyped中包含的the Leaflet type declaration。如果我添加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();

draggabletransform在基本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 Framework:2.0.0
  • Ionic Native:2.4.1
  • Ionic App脚本:1.0.0
  • Angular Core:2.2.1
  • Angular Compiler CLI:2.2.1
  • 节点:6.9.4
  • OS平台:Windows 10
  • Navigator Platform:Win32
  • 用户代理:Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 56.0.2924.87 Safari / 537.36

1 个答案:

答案 0 :(得分:0)

你可以阅读这篇文章http://yunkus.com/angular-error-object-literal-may-only-specify-known-properties/ 它很容易解决,但很难找到原因。使用“any”还是单向的,但它不是最好的解决方案。因为我们只想使用自定义类来创建变量吗?幸运的是,答案已经在那篇文章中有所帮助。你可以阅读它,但这篇文章是由中文写的。希望你能得到它。