- Error
- Reported by the type checker
TS1345
An expression of type 'void' cannot be tested for truthiness.
The result of a `void` function is used as a condition. A `void` value is never meaningful to test; call the function and test something else.
What it means
An expression of type void is used in if, while, &&, ||, ! or a conditional expression. A function typed void returns nothing useful, so testing it is almost always a mistake, such as testing a callback's result instead of calling a predicate.
Example
function save(): void {}
if (save()) {
console.log("saved");
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,5): error TS1345: An expression of type 'void' cannot be tested for truthiness.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Call the function on its own line and test a real condition.
- If the function does return a meaningful value, change its return type (for example to
boolean).