简体   繁体   English

试图通过preventExcessPropertyErrors编译器选项抑制TS2339

[英]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. 我想禁止这种警告的对象的文字,我想,把suppressExcessPropertyErrorstsconfig.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';

More 更多

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';
  1. You will get intellisense support and in next time when you will call x object you will see which properties this object contains. 您将获得智能感知支持,并且在下次调用x对象时,您将看到此对象包含哪些属性。
  2. Good for testing. 适合测试。 You will be sure that construction of your object will not be changed somewhere in your project and can write tests keep in mind it. 您将确保您的对象的构造不会在项目中的某个位置更改,并且可以编写测试来记住它。
  3. You will avoid to overriding your object properties with new ones. 您将避免用新属性覆盖对象属性。 It could be if you declare you object as 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.

相关问题 尝试在 Angular 中添加子 FormGroup 时出现错误 TS2339 - error TS2339 when trying to add child FormGroup in Angular 如何解决TypeScript编译器错误TS2339:我的Angular 5应用程序中类型{}上不存在属性'errorValue' - How to resolve TypeScript compiler error TS2339: Property 'errorValue' does not exist on type {} in my Angular 5 application 打字稿错误-ComponentRef上的TS2339 - Typescript Error - TS2339 on ComponentRef 错误TS2339:JavaScript到Typescript错误 - error TS2339: JavaScript to Typescript error 打字稿:箭头功能-TS2339:类型“ {}”上不存在属性 - Typescript: Arrow function - TS2339: Property does not exist on type '{}' 错误TS2339:类型“无效”上不存在属性“ _ws” - error TS2339: Property '_ws' does not exist on type 'void' 错误TS2339:类型“ {}”上不存在属性“ hometeam” - error TS2339: Property 'hometeam' does not exist on type '{}' 错误 TS2339:“订阅”类型上不存在属性“订阅” - error TS2339: Property 'subscribe' does not exist on type 'Subscription 错误 TS2339:属性“flatMap”在类型“any”上不存在 - error TS2339: Property 'flatMap' does not exist on type 'any 错误 TS2339:“响应”类型上不存在属性“结果” - error TS2339: Property 'results' does not exist on type 'Response'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM