1

I'm using TypeScript 3.7 with @typescript-eslint/parser and @typescript-eslint/eslint-plugin for linting.

I'm trying to use optional chaining syntax and it works fine beside optional calls.

const { hj } = window;
hj?.('formSubmitFailed'); // error

this gives me an error: eslint(no-unused-expressions)

any way to make it work with optional calls?

Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166

2 Answers2

1

One of option is to turn off this rule that produce that error. example how to do it(you could choose off or warn):

 {
   "extends": "./configs/base.json",
   "rules": {
         "@typescript-eslint/no-unused-expressions": "off"
 }}

For more details see eg.: https://github.com/typescript-eslint/typescript-eslint/issues/1423 or how to set rules for typescript-eslint.

SkorpEN
  • 2,491
  • 1
  • 22
  • 28
  • Of course you could also watch releases. They might make it work in version 3.0.0, but you will have to just check when new will be done. – SkorpEN Feb 11 '20 at 05:58
  • 1
    thanks for the link to the issue, but generally I don't want to turn off the rule completely – Tomasz Mularczyk Feb 11 '20 at 07:09
  • So let it as warning. And observe issues or releases. When they will be done you could turn them on once again. In current version they not work so it should not produce error. Warning seems ok. – SkorpEN Feb 11 '20 at 07:34
  • The rule that is being turned off in this answer actually fixes the issue. – epetousis Dec 01 '21 at 05:49
1

Are you use eslint's no-unused-expressions or @typescript-eslint/no-unused-expressions? You have to use the latter.

Try to add this to your config:

rules: {
    'no-unused-expressions': 'off',
    '@typescript-eslint/no-unused-expressions': 2,
},
Shalom Peles
  • 2,285
  • 8
  • 21