Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
670 views
in Technique[技术] by (71.8m points)

javascript - 忽略Typescript错误“属性在类型的值上不存在”(Ignore Typescript Errors “property does not exist on value of type”)

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函数时忽略它。)

  ask by daniel translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(我知道这个问题已经关闭但是我发现它搜索的是同一个TypeScriptException,也许有人在搜索这个问题时遇到了这个问题。)



The problem lays in missing TypeScript typing:

(问题在于缺少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 as those lines can be squqshed into elegant:

(由于TypeScript 1.6首选的投射操作符as那些线条可以被摆成优雅:)

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

    (创建自己的界面,只需扩展HTMLElement)

  2. Introduce own typing which extends HTMLElement

    (引入自己的输入,扩展HTMLElement)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...