类型X的参数不能分配给类型Y的参数.对象文字只能指定已知属性,并且类型Y中不存在X [英] 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

查看:910
本文介绍了类型X的参数不能分配给类型Y的参数.对象文字只能指定已知属性,并且类型Y中不存在X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让Leaflet和一个插件在Ionic 2项目中一起工作.我已经安装并导入Leaflet本身和 leaflet-path-transform 插件,并包含在DefinatelyTyped中的传单类型声明.如果我添加declare var L: any;来忽略类型声明,则代码都可以正常工作.

I'm trying to get Leaflet and a plugin to work together in an Ionic 2 project. I've installed and imported Leaflet itself and the leaflet-path-transform plugin, along with the Leaflet type declaration included in DefinatelyTyped. The code all works if I add declare var L: any; to ignore the type declarations.

这是页面控制器(home.ts)的重要部分:

This is the important parts of the page 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();

draggabletransform在基本Leaflet中不是有效选项(.transform也不是),因此我在PROJECT_DIR\node_modules\@types\leaflet-path-transform\index.d.ts处编写了一个类型声明文件来定义它们.到目前为止,我所写的内容基于传单可编辑的类型声明,因为该插件在创建L.Map对象时允许使用新的MapOptions.

draggable and transform aren't valid options in basic Leaflet (nor is .transform), so I wrote a type declaration file at PROJECT_DIR\node_modules\@types\leaflet-path-transform\index.d.ts to define them. What I've written so far is based on the type declaration for leaflet-editable, since that plugin allows for new MapOptions when creatingthe L.Map object.

/// <reference types="leaflet" />

declare namespace L {

    export interface Polyline {
      draggable: boolean;
    }

    namespace Polyline {
        export interface PolylineOptions {
            draggable: boolean;
        }
    }

}

这仅解决了到目前为止的第一个选项,但未能成功清除TypeScript错误:

This only addresses the first option so far, but it hasn't succeeded in clearing the TypeScript error:

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'.

有人可以帮我弄清楚我在做什么错吗?最初,我认为只是忽略了新类型声明文件,但是如果我在其中输入错字,就会触发新错误,因此似乎确实有效果.

Can someone help me figure out what I'm doing wrong here? At first I thought that the new type declaration file was simply being ignored, but if I leave a typo in it, it triggers new errors, so it does seem to have an effect.

如果有帮助,这是我正在使用的环境:

  • 离子框架:2.0.0
  • 离子原生:2.4.1
  • 离子应用程序脚本:1.0.0
  • 角核:2.2.1
  • Angular编译器CLI:2.2.1
  • 节点:6.9.4
  • OS平台: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/ 它非常容易解决,但很难找到原因.使用"any"仍然是一种方法,但是它不是最佳解决方案.因为我们只想使用自定义类来创建变量,对吗?幸运的是,该帖子中的答案已经更改.您可以阅读,但该帖子是用中文撰写的.希望您可以得到它.

you can read this post http://yunkus.com/angular-error-object-literal-may-only-specify-known-properties/ it's super easy to solve,but hard to find the reason. using the "any" yet is the one way,however it's not the best solution.because we just want to use custom class to creat the variable right? fortunately,answer has alredy in that post.you can read it,but the post is write by chinese.wishing you can get it.

这篇关于类型X的参数不能分配给类型Y的参数.对象文字只能指定已知属性,并且类型Y中不存在X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆