typescript - 让 Prettier 在使用 eslint 时理解 TS "as" 运算符
问题描述
我正在尝试将 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”运算符?请帮帮我。
解决方案
在 .eslintrc 中将解析器添加为“typescript”。
rules: {
"prettier/prettier": ["error", {"singleQuote": true, "parser": "typescript", "endOfLine": "auto"}]
}
这为我修好了。
推荐阅读
- visual-studio - Xamarin Forms Visual Studio 未部署
- java - Wear Google 身份验证所需的最低 SDK
- webpack - 如何通过 webpack 4 压缩图像
- angular - RXJS - Angular - 取消订阅主题
- javascript - 函数 createIdentityMatrix(a,b)
- shiny - 如何根据闪亮应用程序中的选项卡选择实现条件输出
- r - 不知道如何为列表类型的对象自动选择比例。默认为连续
- visual-studio-2017 - 使用 Visual Studio 开发人员提示执行目标
- javascript - 无法让 React 组件显示 onClick
- haskell - 如何在QTree中定义