【问题标题】:Making Prettier understend TS "as" operator when it is working with eslint让 Prettier 在使用 eslint 时理解 TS "as" 运算符
【发布时间】:2020-02-18 10:35:23
【问题描述】:

我正在尝试将 Prettier 与 ESLint 一起用于 TS。但它失败并出现错误

SyntaxError: Unexpected identifier, expected the token `)` (17:55)

在线

const initialState = !process.env.IS_SERVER ? (window as any).__INITIAL_DATA__ : {};

这个错误与 prettier 相关。

我已经为 ES 安装了所有推荐的插件,以便与 prettier 和 TS 一起使用。 ES 配置在这里(.eslingrc.js):

module.exports = {
  parser: "@typescript-eslint/parser", // Specifies the ESLint parser
  extends: [
    "plugin:react/recommended", // Uses the recommended rules from @eslint-plugin-react
    "plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
    "prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
    "plugin:prettier/recommended" // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
  ],
  parserOptions: {
    ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
    sourceType: "module", // Allows for the use of imports
    ecmaFeatures: {
      jsx: true // Allows for the parsing of JSX
    }
  },
  rules: {
    // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
    // e.g. "@typescript-eslint/explicit-function-return-type": "off",
  },
  settings: {
    react: {
      version: "detect" // Tells eslint-plugin-react to automatically detect the version of React to use
    }
  }
};

我使用 IDE WebStorem。 类型的运算符扩展也因 prittier 失败。

为什么不理解“as”运算符? 请帮帮我。

【问题讨论】:

  • @AkashKava 不正确,现在 ESLint 也可以与 Typescript 一起使用,如果使用 @typescript-eslint/parser 设置的话

标签: typescript eslint prettier


【解决方案1】:

在 .eslintrc 中添加 parser 作为“打字稿”。

rules: {
  "prettier/prettier": ["error", {"singleQuote": true, "parser": "typescript", "endOfLine": "auto"}]
}

这为我解决了问题。

【讨论】:

    【解决方案2】:

    检查,prettier 文件配置扩展名 (.prettierrc.json)

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2017-11-25
      • 2021-05-06
      • 2020-06-18
      • 2021-06-01
      • 1970-01-01
      • 2022-01-09
      • 2020-12-25
      • 2021-11-23
      相关资源
      最近更新 更多