[英]Trying to suppress TS2339 by suppressExcessPropertyErrors compiler option
Here is the code: 这是代码:
var x = {};
x.test = 'abc';
Getting typescript compiler error: 获取打字稿编译器错误:
TS2339: Property 'test' does not exist on type '{}'.
TS2339:类型“ {}”上不存在属性“ test”。
I want to suppress this warning for object literals, I suppose that putting suppressExcessPropertyErrors
into tsconfig.json
should solve this. 我想禁止这种警告的对象的文字,我想,把
suppressExcessPropertyErrors
到tsconfig.json
应该解决这个问题。
tsconfig: tsconfig:
{
"compilerOptions": {
"suppressExcessPropertyErrors": true
},
...
}
But nothing changed.. compiler still showing the error. 但是什么都没有改变..编译器仍然显示错误。
Thank you for any hints ;) 谢谢你的提示;)
I suppose that putting suppressExcessPropertyErrors into tsconfig.json should solve this.
我猜想,将preventExcessPropertyErrors放入tsconfig.json中应该可以解决此问题。
No. It suppresses excess properties in object construction eg 不。它可以抑制对象构造中的多余属性,例如
var x = {};
x = {test:'abc'};
I want to suppress this warning for object literals
我想针对对象文字禁止显示此警告
You can do anything you want using the any
type eg 您可以使用
any
类型来执行任何您想做的事情,例如
var x:any = {};
x.test = 'abc';
This is called lazy object initiliazation and patterns for dealing with it are covered here : https://basarat.gitbooks.io/typescript/content/docs/tips/lazyObjectLiteralInitialization.html 这称为惰性对象初始化,并且在此处介绍了处理它的模式: https : //basarat.gitbooks.io/typescript/content/docs/tips/lazyObjectLiteralInitialization.html
I could agree with @basarat by some points, but please let me to add some suggestions also. 我可以在某些方面同意@basarat ,但也请允许我添加一些建议。
Declaration object props like this could a make sense by several reasons: 这样的声明对象道具可能有几个道理:
var x = {
test: ''
};
x.test = 'abc';
x
object you will see which properties this object contains. x
对象时,您将看到此对象包含哪些属性。 any
type. any
类型。 Explanation : 说明 :
// origin object x with originProp property
var x: any = {
originProp: 'abc'
};
// new object with new newProp property
x = {
newProp:'abc'
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.