简体   繁体   English

是否有关于可选属性的属性访问的 Typescript-Eslint 规则?

[英]Is there a Typescript-Eslint rule on property access on optional property?

interface TypeA {
    optionalObj?: { prop: string }
}

const a: TypeA = {};

console.log(a.optionalObj.prop);

I want to find a Typescript-Eslint rule that will error on the line a.optionalObj.prop , because optionalObj can be undefined.我想找到一个 Typescript-Eslint 规则,它会在a.optionalObj.prop行出错,因为 optionalObj 可以是未定义的。

Is there such a rule?有这样的规定吗?

This should be caught as a TS Error TS2532: Object is possibly 'undefined'.这应该被捕获为 TS 错误TS2532: Object is possibly 'undefined'.

If you're not seeing this error, fix your tsconfig.json .如果您没有看到此错误,请修复您的tsconfig.json You should turn strictNullChecks on.您应该打开strictNullChecks Consider below flags.考虑下面的标志。

   "strict": true,
    "alwaysStrict": true,

暂无
暂无

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

相关问题 如何处理 catch(err) 块上的 @typescript-eslint/no-unsafe-member-access 规则? - How to handle the @typescript-eslint/no-unsafe-member-access rule on catch(err) blocks? 如何禁用@typescript-eslint/no-non-null-assertion 规则 - How to disable @typescript-eslint/no-non-null-assertion rule 未找到规则“@typescript-eslint/no-redeclare”的定义 - Definition for rule '@typescript-eslint/no-redeclare' was not found Vue 3 @typescript-eslint/no-unsafe-member-access - Vue 3 @typescript-eslint/no-unsafe-member-access Typescript:从方法访问可选的 class 属性 - Typescript: Access optional class property from method 添加规则时出现 Eslint 错误 @typescript-eslint/interface-name-prefix - Eslint erro when adding rule @typescript-eslint/interface-name-prefix Angular & eslint - 未找到规则“@typescript-eslint/space-infix-ops”的定义 - Angular & eslint - Definition for rule '@typescript-eslint/space-infix-ops' was not found typescript-eslint 未使用 tsconfig - tsconfig not used by typescript-eslint 如何修复“未找到规则‘@typescript-eslint/no-use-before-declare’的定义。eslint(@typescript-eslint/no-use-before-declare)” - How to fix 'Definition for rule '@typescript-eslint/no-use-before-declare' was not found.eslint(@typescript-eslint/no-use-before-declare)' 如何解决“未找到规则‘@typescript-eslint/rule-name’的定义” - How to resolve "Definition for rule '@typescript-eslint/rule-name' was not found"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM