typescript-eslint: Bug: [no-unused-expressions] false negative on satisfies expression

Before You File a Bug Report Please Confirm You Have Done The Following…

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play#ts=5.4.5&fileType=.tsx&code=IwAgzghgLglmBmMCmYQDsCuBbARkgTgNwBQQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1tidmUQAm9AB7FoKZJQ5NUGSImjQO0SODABfEOqA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

1 satisfies number;

ESLint Config

module.exports = {
  "rules": {
    "@typescript-eslint/no-unused-expressions": "error"
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

Error

Actual Result

No error

Additional Info

This was discovered while working on https://github.com/typescript-eslint/typescript-eslint/pull/9199. I will send a PR when that doc PR is merged.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 19 (15 by maintainers)

Most upvoted comments

thing satisfies never; is objectively an unused expression, no different than the other 3.

Well, no, it is only unused at runtime, but used at typechecking time, unlike the other three that are unused both at runtime and at typechecking time.

@Josh-Cena Green light from my side to proceed with the PR under this plan.

ESLint lints JavaScript. In JavaScript, an expression either has effects at runtime, or no effect at all. We lint TypeScript. In TypeScript, an expression can have effects at runtime, compile-time, or no effect at all.

Currently, we validate assertions and instantiation expressions by checking whether they have runtime effects, although arguably assertions have compile-time effects too. This is arguably wrong because a as number is equally terrible as a() as number; you would only want to write a() in the latter case without as number.

It’s not clear how many people rely on the compile-time effects of satisfies, but an option similar to ESLint’s allowTaggedTemplates etc. would be useful, except we would be more nuanced and let users specify whether (a) it should always be considered unnecessary (b) it should always be considered necessary © it should be considered unnecessary if it has no runtime effects (current behavior).

Both the default: and the unused expression are unreachable/dead runtime code

They may be. Until you add a new value to the union/enum and then they’re not. It’s a convenient way to get that notification that you forgot to update a switch - especially if that switch is “non-terminal” (eg it doesn’t have a return statement and isn’t the last statement in a function body).

They also may not be truly unreachable eg if you’re switching over server responses - the JS code may not be in sync with the server code. But if that’s the usecase an explicit crash is usually preferred rather than an implicit no-op.

You can simply use switch-exhaustiveness-check instead

If you use type-aware linting and want every switch to be exhaustive - then you’re correct. But there are many codebases where one or both of these constraints does not hold true.

Using satisfies requires no lint rules and is thus “free” (so long as you remember it)

and odd use of satisfies

It’s a pretty idiomatic one for type-system checking of things without runtime crashes

For example

switch (thing) {
  case 1: break;
  case 2: break;
  case 3: break;

  default:
    thing satisfies never;
    // enforces at the type-system level only that the switch is exhaustive.
} 

The alternative is writing a function that accepts an argument of type never - which is much heavier than just using the satisfies operator.

The expression is a no-op and will likely be just ignored by the engine during parsing. Any minifier run over your code would also likely cull the expression entirely.

1 satisfies number;

is transpiled to:

1;

This is clearly an unused expression (and odd use of satisfies).

My personal intuition is that we should unconditionally report assertions and instantiation expressions and always allow satisfies, but with options to toggle each ("ignore" | "report" | "transparent").