typescript - 设置 tsconfig 忽略 "Property ' foo' does not exist on type 'bar' ."but only for a specific variable

标签 typescript lint tslint tsconfig

我有一个对象 foo,它在整个代码库中使用,需要从中访问属性 bar。但是由于具体情况ts认为property不存在于其上,尽管实际上是存在的。所以我到处都收到这条消息的红线:

Property 'foo' does not exist on type 'bar'

在我的 ts-config 文件中,我想将其设置为忽略所有这些错误,但仅限于 foo 变量。

这可以做到吗?

最佳答案

你不能在tsconfig中这样做,没有这样的设置。您可以执行以下几项操作之一。

any 使用类型断言。使用此所有属性访问都是允许的:

(bar as any).foo

另一种选择是只抑制错误。您可以使用 @ts-ignore 来抑制下一行的所有错误

//@ts-ignore
bar.foo // no error

Playground Link

注意:您最好找到错误的原因,或者增加类型或将变量的类型充电到包含 foo 但没有更多内容的内容细节难以提出解决方案。

关于typescript - 设置 tsconfig 忽略 "Property ' foo' does not exist on type 'bar' ."but only for a specific variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59989890/

相关文章:

typescript :允许泛型类型只能是具有 'string' 属性的对象

javascript - Angular 中 tsconfig.json 文件的 lib 数组中的 dom 是什么?

java - Android Studio 如何显示 "Parcelable implementation needs CREATOR field"错误?

typescript - 在 Ionic 2 应用程序中从 TSLint 中排除 node_modules

typescript TS7053 : Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'

typescript - 如何在 Visual Studio 2019 错误列表中显示 eslint 错误

php - PHP Web 应用程序的服务器端性能测试

css - "@extend must be used with a %placeholder"是什么意思?

angular - 将 WebStorm TSLint 错误行更改为警告行

typescript - npm test 在运行 Protractor typescript 时抛出错误