使更漂亮的 TS 在与 eslint 合作时"as"运算符



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

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

与联机

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

这个错误与更漂亮有关。

我已经为ES安装了所有推荐的插件,以便与Beautier和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"操作符?请帮帮我。

如果使用.prettierrc文件,则将"parser": "typescript"添加到对象中。

{
"tabWidth": 2,
"singleQuote": true,
"parser": "typescript"    <<----
}

如果你没有使用.prettierrc文件,那么@Steve Tomlin的答案就可以了。

在.eslintrc中,将解析器添加为"typescript";。

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

这帮我搞定了。

检查,更漂亮文件配置扩展名(.pareterrc.json(

相关内容

  • 没有找到相关文章

最新更新