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
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)
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 numberis equally terrible asa() as number; you would only want to writea()in the latter case withoutas number.It’s not clear how many people rely on the compile-time effects of
satisfies, but an option similar to ESLint’sallowTaggedTemplatesetc. 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).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
returnstatement 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.
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)
It’s a pretty idiomatic one for type-system checking of things without runtime crashes
For example
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.
is transpiled to:
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").