typescript 忽略打字稿错误“类型的值上不存在属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18083389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:07:36  来源:igfitidea点击:

Ignore Typescript Errors "property does not exist on value of type"

javascripttypescriptvisual-studio-2013

提问by daniel

In VS2013 building stops when tsc exits with code 1. This was not the case in VS2012.

在 VS2013 中,当 tsc 以代码 1 退出时,构建会停止。在 VS2012 中情况并非如此。

How can I run my solution while ignoring the tsc.exe error?

如何在忽略 tsc.exe 错误的同时运行我的解决方案?

I get many The property 'x' does not exist on value of type 'y'errors, which I want to ignore when using javascript functions.

我收到很多The property 'x' does not exist on value of type 'y'错误,在使用 javascript 函数时我想忽略这些错误。

回答by michalczukm

I know the question is already closed but I've found it searching for same TypeScriptException, maybe some one else hit this question searching for this problem.

The problem lays in missing TypeScript typing:

我知道这个问题已经结束,但我发现它在搜索相同的 TypeScriptException,也许其他人在搜索这个问题时遇到了这个问题。

问题在于缺少 TypeScript 类型:

var coordinates = outerElement[0].getBBox();

Throws The property 'getBBox' does not exist on value of type 'HTMLElement'.

投掷 The property 'getBBox' does not exist on value of type 'HTMLElement'.


The easiest way is to explicitly type variable as any


最简单的方法是将变量显式键入为 any

var outerHtmlElement: any = outerElement[0];
var coordinates = outerHtmlElement.getBBox();

Edit, late 2016

编辑,2016 年底

Since TypeScript 1.6 prefered casting operator is asthose lines can be squqshed into elegant:

由于 TypeScript 1.6 首选的强制转换运算符是as这些行可以被压缩成优雅的:

let coordinates = (outerElement[0] as any).getBBox();

let coordinates = (outerElement[0] as any).getBBox();



Other solutions

其他解决方案

Of course if you'd like to do it right, which is an overkill sometimes, you can:

当然,如果你想把它做对,这有时有点矫枉过正,你可以:

  1. Create own interface which simply extends HTMLElement
  2. Introduce own typing which extends HTMLElement
  1. 创建自己的接口,简单地扩展 HTMLElement
  2. 引入自己的类型,扩展 HTMLElement

回答by Bruno Grieder

The quick and dirty solution is to explicitly cast to any

快速而肮脏的解决方案是明确地强制转换为 any

(y as any).x

The "advantage" is that, the cast being explicit, this will compile even with the noImplicitAnyflag set.

“优点”是,强制转换是显式的,即使noImplicitAny设置了标志,它也会编译。

The proper solution is to update the typings definition file.

正确的解决方案是更新typings 定义文件。

Please note that, when you cast a variable to any, you opt outof type checking for that variable.

请注意,当您将变量强制转换为 时any,您选择不对该变量进行类型检查。



Since I am in disclaimer mode, double casting via anycombined with a new interface, can be useful in situations where you

由于我处于免责声明模式,通过any结合新界面进行双重转换,在您

  • do not want to update a broken typings file
  • are monkey patching
  • 不想更新损坏的打字文件
  • 猴子修补

yet, you still want some form of typing.

然而,您仍然需要某种形式的打字。

Say you want to patch the definition of an instance of yof type OrginalDefwith a new property xof type number:

说要修补的一个实例的定义y类型的OrginalDef一个新的属性x类型number

const y: OriginalDef = ...

interface DefWithNewProperties extends OriginalDef {
    x: number
}

const patched = y as any as DefWithNewProperties

patched.x = ....   //will compile

回答by Yaroslav Yakovlev

You can also use the following trick:

您还可以使用以下技巧:

y.x = "some custom property"//gives typescript error

y.x = "some custom property"//gives typescript error

y["x"] = "some custom property"//no errors

y["x"] = "some custom property"//no errors

Note, that to access xand dont get a typescript error again you need to write it like that y["x"], not y.x. So from this perspective the other options are better.

请注意,要访问x并且不再出现打字稿错误,您需要像这样编写它y["x"],而不是y.x. 所以从这个角度来看,其他选择更好。

回答by Charles Marsh

There are several ways to handle this problem. If this object is related to some external library, the best solution would be to find the actual definitions file (great repository here) for that library and reference it, e.g.:

有几种方法可以处理这个问题。如果此对象与某个外部库相关,最好的解决方案是找到该库的实际定义文件(这里是一个很好的存储库)并引用它,例如:

/// <reference path="/path/to/jquery.d.ts" >

Of course, this doesn't apply in many cases.

当然,这在很多情况下并不适用。

If you want to 'override' the type system, try the following:

如果要“覆盖”类型系统,请尝试以下操作:

declare var y;

This will let you make any calls you want on var y.

这将使您可以拨打任何您想要的电话var y

回答by Benny Neugebauer

When TypeScript thinks that property "x"does not exist on "y", then you can always cast "y" into "any", which will allow you to call anything (like "x") on "y".

当打字稿认为,财产的“X”上不存在“Y”,那么你可以随时施放“Y”为“任何”,这将允许您调用任何东西(如“x”)上的“Y”。

Theory

理论

(<any>y).x;

Real World Example

真实世界示例

I was getting the error "TS2339: Property 'name' does not exist on type 'Function'" for this code:

对于此代码,我收到错误“TS2339:类型 'Function' 上不存在属性 'name'”:

let name: string = this.constructor.name;

So I fixed it with:

所以我修复了它:

let name: string = (<any>this).constructor.name;

回答by Avram Virgil

Had a problem in Angular2, I was using the local storage to save something and it would not let me.

在 Angular2 中遇到问题,我正在使用本地存储来保存某些内容,但它不允许我这样做。

Solutions:

解决方案:

I had localStorage.city -> error -> Property 'city' does not exist on type 'Storage'.

我有 localStorage.city -> error -> Property 'city' does not exist on type 'Storage'.

How to fix it:

如何修复:

localStorage['city']

(localStorage).city

(localStorage as any).city

localStorage['城市']

(localStorage).city

(localStorage as any).city

回答by danday74

A quick fix where nothing else works:

一个没有其他作用的快速修复:

const a.b = 5 // error

const a['b'] = 5 // error if ts-lint rule no-string-literal is enabled

const B = 'b'
const a[B] = 5 // always works

Not good practice but provides a solution without needing to turn off no-string-literal

不是很好的做法,但提供了一种无需关闭无字符串文字的解决方案

回答by Lewis

I know it's now 2020, but I couldn't see an answer that satisfied the "ignore" part of the question. Turns out, you can tell TSLint to do just that using a directive;

我知道现在是 2020 年,但我看不到满足问题“忽略”部分的答案。事实证明,您可以使用指令告诉 TSLint 这样做;

// @ts-ignore
this.x = this.x.filter(x => x.someProp !== false);

Normally this would throw an error, stating that 'someProp does not exist on type'. With the comment, that error goes away.

通常这会抛出一个错误,指出'someProp 在类型上不存在'。随着评论,该错误消失了。

This will stop any errors being thrown when compiling and should also stop your IDE complaining at you.

这将停止编译时抛出的任何错误,并且还应该停止您的 IDE 向您抱怨。

回答by cs_pupil

I was able to get past this in typescript using something like:

我能够使用以下内容在打字稿中解决这个问题:

let x = [ //data inside array ];
let y = new Map<any, any>();
for (var i=0; i<x.length; i++) {
    y.set(x[i], //value for this key here);
}

This seemed to be the only way that I could use the values inside X as keys for the map Y and compile.

这似乎是我可以使用 X 中的值作为映射 Y 的键并进行编译的唯一方法。

回答by Randy

In my particular project I couldn't get it to work, and used declare var $;. Not a clean/recommended solution,it doesnt recognise the JQuery variables, but I had no errors after using that (and had to for my automatic builds to succeed).

在我的特定项目中,我无法让它工作,并使用declare var $;. 不是一个干净/推荐的解决方案,它不能识别 JQuery 变量,但使用后我没有错误(并且必须让我的自动构建成功)。