[英]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.