禁止以感叹号作为后缀的方式使用非空断言。
规则配置
// code-linter.json5
{
"rules": {
"@typescript-eslint/no-non-null-assertion": "error"
}
} 正例
interface Example {
property?: string;
}
declare const example: Example;
export const includesBaz = example.property?.includes('baz') ?? false; 反例
interface Example {
property?: string;
}
declare const example: Example;
// 禁止使用"example.property!"的方式来进行非空断言
export const includesBaz = example.property!.includes('baz');