3
interface Address {
  street: string,
}

export const getAddress = (address: Address | null) : string =>
  address?.street ? `${address?.street}`
    : '0000 Default Dr';

Does anybody know am I getting Parsing error: Expression expected. eslint on address in address?.street ? ?

Eslint rules both for the .js and .ts:

 {
      "extends": [
        "plugin:cypress/recommended",
        "plugin:@typescript-eslint/recommended"
      ],
      "overrides":
        {
          "files": ["**/*.{ts,tsx}"],
          "parser": "@typescript-eslint/parser",
          "parserOptions": {
            "project": "./tsconfig.json"
          },
          "plugins": ["@typescript-eslint", "prettier", "react-hooks", "jsx-a11y"],
          "extends": [
            "eslint:recommended",
            "react-app",
            "plugin:@typescript-eslint/recommended",
            "plugin:@typescript-eslint/recommended-requiring-type-checking",
            "prettier"
          ],
          "rules": {
            "@typescript-eslint/explicit-function-return-type": "off",
            "@typescript-eslint/no-explicit-any": "off",
            "@typescript-eslint/no-shadow": "off",
            "no-empty-function": 0,
            "@typescript-eslint/no-empty-function": "off",
            "@typescript-eslint/explicit-module-boundary-types": "off",
            "@typescript-eslint/no-non-null-assertion": "off",
            "no-use-before-define": "off",
            "@typescript-eslint/no-use-before-define": ["error"]
          },
          "settings": {
            "import/parsers": {
              "@typescript-eslint/parser": [".ts", ".tsx"]
            },
            "import/resolver": {
              "typescript": {}
            },
            "react": {
              "version": "detect"
            }
          }
        }
      ]
    }
0

2 Answers 2

2

It looks like the ES version is set to the default value. Optional chaining (address?.street) is ES2019. By default, ESLint expects ECMAScript 5 syntax. If you want to enable ES2019 syntax add this to parserOptions: ecmaVersion: '9' (or '2019') to your eslintrc (link to docs).

Sign up to request clarification or add additional context in comments.

Comments

0

Multiple things can cause this error -> link to article

In your case declaring const and variable export in different lines would work.

Apart from what is mentioned in the linked article, and specific to my case, I got this error when chaining statements without brackets and adding an in-line comment at the end.

This was the original statement I wrote that throws the error:

if (selectedHour < startHour - 1) for (let i = 0; i <= 60; i++) elapsedMinutes.push(i); // disable all minutes

The solution was to remove the comment from the end and place it on a separate line:

// disable all minutes
if (selectedHour < startHour - 1) for (let i = 0; i <= 60; i++) elapsedMinutes.push(i);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.