Skip to main content

no-extra-non-null-assertion

Disallow extra non-null assertions.

ESLint 配置 中扩展"plugin:@typescript-eslint/recommended" 可启用此规则。

🔧

此规则报告的一些问题可通过 --fix ESLint 命令行选项自动修复

TypeScript 中的 ! 非空断言运算符用于断言值的类型不包括 nullundefined。对单个值多次使用该运算符不会产生任何效果。

¥The ! non-null assertion operator in TypeScript is used to assert that a value's type does not include null or undefined. Using the operator any more than once on a single value does nothing.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-extra-non-null-assertion": "error"
}
});

在线运行试试这个规则 ↗

示例

¥Examples

const foo: { bar: number } | null = null;
const bar = foo!!!.bar;
Open in Playground
function foo(bar: number | undefined) {
const bar: number = bar!!!;
}
Open in Playground
function foo(bar?: { n: number }) {
return bar!?.n;
}
Open in Playground

选项

该规则不可配置。

'## 资源'