Typescript-eslint: [no-unused-expressions] Incompatibility with optional chaining a function

Created on 24 Jan 2020  路  3Comments  路  Source: typescript-eslint/typescript-eslint

no-unused-expressions lint error is thrown when optional chaining a function evaluation, i.e. fn?.(). This should not occur since it is not an expression.

const fn = (optionalFnParameter?: () => void): void => {
  optionalFnParameter?.(); // LINT ERROR
};

Expected Result
No exception, because the function is not returning anything

Actual Result
"no-unused-expressions" lint error

| package | version |
| ---------------------------------- | ------- |
| @typescript-eslint/eslint-plugin | 2.16.0 |
| @typescript-eslint/parser | 2.16.0 |
| TypeScript | 3.7.5 |
| ESLint | 5.16.0 |
| node | 12.8.1 |
| npm | `6.10.2 |

eslint-plugin working as intended

Most helpful comment

Patch the config here:

rules: {
+  'no-unused-expressions': 'off',
+  '@typescript-eslint/no-unused-expressions': 2,
},

All 3 comments

Make sure you are using our extension rule.
https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/FAQ.md#i-am-using-a-rule-from-eslint-core-and-it-doesnt-work-correctly-with-typescript-code

Patch the config here:

rules: {
+  'no-unused-expressions': 'off',
+  '@typescript-eslint/no-unused-expressions': 2,
},

Just as a note, if you were using the recommended config the patch @zombieJ suggested is not present. It is present already if you are using the all config.

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/all.json#L64-L65

Was this page helpful?
0 / 5 - 0 ratings