typescript 解析错误:应为表达式,埃斯林特

velaa5lx  于 2023-05-19  发布在  TypeScript
关注(0)|答案(2)|浏览(409)
interface Address {
  street: string,
}

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

有人知道我在address?.street ?中的address上得到Parsing error: Expression expected. eslint吗?
Eslint针对.js.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"
            }
          }
        }
      ]
    }
ldxq2e6h

ldxq2e6h1#

看起来ES版本设置为默认值。可选的链接(address?.street)是ES2019。默认情况下,ESLint需要ECMAScript 5语法。如果你想启用ES2019语法,请将以下内容添加到parserOptionsecmaVersion: '9'(或'2019')到eslintrc(链接到文档)。

b09cbbtk

b09cbbtk2#

有多种原因可能导致此错误-> link to article
在你的例子中,在不同的行中声明const和variable export是可行的。
除了在链接的文章中提到的,以及具体到我的情况,我得到了这个错误,当我链接语句不使用括号,并在最后添加了一个行内注解。
这是我写的抛出错误的原始语句:

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

解决方案是将注解从末尾删除,并将其放在单独的一行:

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

希望这有帮助!编码快乐!

相关问题