Tslint: Ternary with null value marked as unused expression

Created on 13 Apr 2018  路  2Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.9.1
  • __TypeScript version__: 2.7.2
  • __Running TSLint via__: VSCode

TypeScript code being linted

onEnter = (e: KeyboardEvent) => {
    e.keyCode === 13 ? fetchData(url) : null;
};

with tslint.json configuration:

"no-unused-expressions": [true, "allow-fast-null-checks"],

Actual behaviour

Referring to the null value -[tslint] unused expression, expected an assignment or function call (no-unused-expression)

Expected behaviour

Allow ternary operations that evaluate to null

Not A Bug

Most helpful comment

Ternary operator shorthand is a natural use case here, we don't care about the false case. In comparison if condition is just noise.

if (cond) {
  effect()
}

cond ? effect() : null

All 2 comments

@james-prado this behavior is completely expected. null; by itself is rejected with the same error. use an if statement for this pattern.

Ternary operator shorthand is a natural use case here, we don't care about the false case. In comparison if condition is just noise.

if (cond) {
  effect()
}

cond ? effect() : null
Was this page helpful?
0 / 5 - 0 ratings