Disallow non-null assertion in locations that may be confusing.
Using a non-null assertion (!) next to an assign or equals check creates code that is confusing as it looks similar to a not equals check.
// code-linter.json5
{
"rules": {
"@typescript-eslint/no-confusing-non-null-assertion": "error"
}
} interface Foo {
bar?: string;
num?: number;
}
function getFoo(): Foo {
return {
bar: 'bar',
num: Number.MAX_VALUE
};
}
const foo: Foo = getFoo();
export const isEqualsBar = foo.bar === 'hello';
const num = 2;
export const isEqualsNum = num + (foo.num!) === num; interface Foo {
bar?: string;
num?: number;
}
function getFoo(): Foo {
return {
bar: 'bar',
num: Number.MAX_VALUE
};
}
const foo: Foo = getFoo();
// Confusing. It looks similar to a not equals check.
export const isEqualsBar = foo.bar! === 'hello';
// Confusing. It looks similar to a not equals check.
const num = 2;
export const isEqualsNum = num + foo.num! === num; plugin:@typescript-eslint/all
For details about how to configure Code Linter check rules, see Code Linter.