Skip to main content

no-unused-expressions

Disallow unused expressions.

This rule extends the base eslint/no-unused-expressions rule. It supports TypeScript-specific expressions:

  • Marks directives in modules declarations ("use strict", etc.) as not unused
  • Marks the following expressions as unused if their wrapped value expressions are unused:
    • Assertion expressions: x as number;, x!;, <number>x;
    • Instantiation expressions: Set<number>;

Although the type expressions never have runtime side effects (that is, x!; is the same as x;), they can be used to assert types for testing purposes.

Examples

Set<number>;
1 as number;
window!;
Open in Playground

Options

See eslint/no-unused-expressions's options.

How to Use

.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-unused-expressions": "off",
"@typescript-eslint/no-unused-expressions": "error"
}
};

Try this rule in the playground ↗

Resources

Taken with ❤️ from ESLint core.