Skip to main content

no-non-null-assertion

Disallow non-null assertions using the ! postfix operator.

🔒

Extending "plugin:@typescript-eslint/strict" in an ESLint configuration enables this rule.

💡

Some problems reported by this rule are manually fixable by editor suggestions.

TypeScript's ! non-null assertion operator asserts to the type system that an expression is non-nullable, as in not null or undefined. Using assertions to tell the type system new information is often a sign that code is not fully type-safe. It's generally better to structure program logic so that TypeScript understands when values may be nullable.

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

Try this rule in the playground ↗

Examples

interface Example {
property?: string;
}

declare const example: Example;
const includesBaz = example.property!.includes('baz');
Open in Playground

Options

This rule is not configurable.

When Not To Use It

If your project's types don't yet fully describe whether certain values may be nullable, such as if you're transitioning to strictNullChecks, this rule might create many false reports. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.

Resources