在VS2013中,当TSC以代码1退出时,建筑物停止。在VS2012中不是这种情况。
如何在忽略tsc.exe错误时运行解决方案? 我有很多The property 'x' does not exist on value of type 'y'
我知道这个问题已经关闭,但是我发现它正在寻找相同的类型criptexception,也许还有其他人打了这个问题,以搜索此问题。
var coordinates = outerElement[0].getBBox();
throws
The property 'getBBox' does not exist on value of type 'HTMLElement'.
最简单的方法是将变量明确键入为`
var outerHtmlElement: any = outerElement[0];
var coordinates = outerHtmlElement.getBBox();
eDit,2016年下半年
as
,因此可以将这些行挤压到:let coordinates = (outerElement[0] as any).getBBox();
当然,如果您想做对的话,这是有时的过度杀伤,您可以:
HTMLElement
向扩展的键入
HTMLElement
快速而肮脏的解决方案是明确施放到
any
“优势”是,演员表是显式的,即使是
noImplicitAny
旗设置也会编译。
适当的解决方案是更新打字定义文件。
请注意,当您将变量施放为any
时,您的类型检查该变量。
由于我处于免责声明模式,通过新界面结合了双重铸造,在情况下很有用您不想更新损坏的打字文件
和/或您是猴子补丁
thyet,您仍然需要某种形式的打字。tay您想用新的属性
any
:::
y
OrginalDef
x
注意,要访问
number
而又不要再遇到打字稿错误,您需要像这样编写它,而不是
// The `y` variable has a typing `OriginalDef` missing a property `x`
const y: OriginalDef = ...
// create an interface with your new property(ies)
interface DefWithNewProperties extends OriginalDef {
x: number
}
// patch the current `y` to the new interface using double casting
const patched = y as any as DefWithNewProperties
// the patched value can now access the new property
// the type inference and type checker will work correctly
patched.x = .... //will compile
。因此,从这个角度来看,其他选项更好。
有几种解决此问题的方法。如果此对象与某些外部库有关,则最好的解决方案是为该库找到实际的定义文件(大存储库here)并引用它,例如:
y.x = "some custom property"//gives typescript error
当然,这在许多情况下不适用。
如果您想“覆盖”类型系统,请尝试以下内容:
y["x"] = "some custom property"//no errors
这将使您在
x
中打电话。
我知道现在是2020年,但是我看不到满足问题的“忽略”部分的答案。事实证明,您可以告诉TSLINT使用指令;
y["x"]
通常,这将丢弃一个错误,并指出“类型上不存在某些prop”。通过评论,该错误消失了。 这将停止在编译时丢下任何错误,也应阻止您的IDE抱怨您。
当打字稿认为属性“ x”不存在于
“ y”时,您总是可以将“ y”施加到“任何”中,这将使您在“ y”上调用任何东西(例如“ x”) ”
theory
y.x
真实的世界例子
I在此代码上获取错误“ TS2339:属性'名称'不存在”:/// <reference path="/path/to/jquery.d.ts" >
我用以下方式修复了它:
declare var y;
在Angular2中有一个问题,我正在使用本地存储来保存一些东西,这不会让我。 solutions:
var y
如何解决: localstorage ['City']
(localstorage).city (localstorage作为任何).city
在没有其他工作的地方快速修复:
// @ts-ignore
this.x = this.x.filter(x => x.someProp !== false);
不好练习,但提供了解决方案,而无需关闭无弦线
在我的特定项目中,我无法正常工作并使用了
(<any>y).x;
。
我能够使用以下内容在打字稿中克服这一点:
let name: string = this.constructor.name;
这似乎是我使用x内部值作为地图y和编译的键的唯一方法。