简体   繁体   English

有没有办法放松 TypeScript 编译器警告 TS2339?

[英]Is there a way to relax TypeScript compiler warning TS2339?

I am using the TypeScript compiler to check my JavaScript code as well.我也在使用 TypeScript 编译器来检查我的 JavaScript 代码。 The reason for this is to find possible defects as early as possible.这样做的原因是尽早发现可能的缺陷。 One of the compiler warnings of the TypeScript compiler is TS2339. TypeScript 编译器的编译器警告之一是 TS2339。 It checks whether a type has defined a certain property.它检查一个类型是否定义了某个属性。 Since this can easily go wrong in JavaScript, this is a valuable compiler warning.由于这很容易在 JavaScript 中出错,因此这是一个有价值的编译器警告。 For instance, suppose I have the following type:例如,假设我有以下类型:

mycar = {

  color: "green",

  topspeed: 180

};

and I am using somewhere in my code我在代码中的某个地方使用

c = mycar.colour;

I will get a warning that "colour" doesn't exist (typo).我会收到“颜色”不存在的警告(错字)。 This would have gone unnoticed if I didn't have run the TypeScript compiler.如果我没有运行 TypeScript 编译器,这将被忽视。 So far so good.到目前为止,一切都很好。

But suppose you have the following JavaScript code, which is a common way of doing things in JavaScript:但是假设你有如下 JavaScript 代码,这是 JavaScript 中常用的处理方式:

mycar = {};

mycar.color = "green";

mycar.topspeed = 180;

If you run the TypeScript compiler then you will get如果你运行 TypeScript 编译器,那么你会得到

error TS2339: Property 'color' does not exist on type 'mycar'

Strictly speaking the TypeScript compiler is right, but actually nothing can go wrong here.严格来说 TypeScript 编译器是对的,但实际上 go 在这里没有什么错。 You might argue why I didn't write the code as the first example, but this is not common in JavaScript and moreover in reality these data structures are far more complicated and in such a case the latter way of coding is regarded as more convenient.您可能会争辩为什么我没有编写代码作为第一个示例,但这在 JavaScript 中并不常见,而且实际上这些数据结构要复杂得多,在这种情况下,后一种编码方式被认为更方便。

So my question is: is there a possibility to relax the TypeScript compiler warning TS2339 to only spot the real issues?所以我的问题是:是否有可能放宽 TypeScript 编译器警告 TS2339 以仅发现真正的问题?

I think this chapter of wonderful Typescript book by Basarat might answer your question better than I am.我认为 Basarat 的精彩 Typescript 书的这一章可能会比我更好地回答你的问题。 It is quite short and 100% on point它很短,而且 100% 准确

https://basarat.gitbook.io/typescript/main-1/lazyobjectliteralinitialization https://basarat.gitbook.io/typescript/main-1/lazyobjectliteralinitialization

How is Typescript supposed to know where the real issues are and which you know of ? Typescript 应该如何知道真正的问题在哪里以及您知道哪些? It actually works the other way round, you can tell typescript at which places it should not safeguard you:它实际上反过来工作,你可以告诉 typescript 在哪些地方它不应该保护你:

  mycar = {} as any;

Or you could also work with the compiler and properly type the variable:或者您也可以使用编译器并正确键入变量:

  let mycar: { color: number, /*...*/ };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2339:JavaScript到Typescript错误 - error TS2339: JavaScript to Typescript error Typescript 无法编译类型不存在的错误 TS2339 - Typescript can't compile type not existing Error TS2339 如何解决打字稿错误 TS2339:“IntrinsicAttributes & …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …? Typescript TS2339 属性在类型上不存在 - 类声明 - Typescript TS2339 Property doesn't exist on type - Class decleration TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339 为什么在键入Redux-saga调用参数时Typescript会引发错误-TS2339 - Why does Typescript throw an error when typing Redux-saga call parameter - TS2339 打字稿错误TS2339:'Window'类型中不存在属性'webkitURL' - Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window' TypeScript:错误TS2339:类型“ Window”上不存在属性“ CustomEvent” - TypeScript: error TS2339: Property 'CustomEvent' does not exist on type 'Window' ts2339怎么解决? 使用带有测试库/反应的打字稿 - How to solve ts2339? Using typescript with testing-library/react 如何在Typescript中为其子项设置功能道具类型? TS2339类型上不存在“孩子”属性 - How to set function props type for its child in Typescript? Property 'children' does not exist on type TS2339
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM