简体   繁体   English

Typescript 铸造与 babel eslint 解析

[英]Typescript casting with babel eslint parsing

I am getting some parsing errors after introducing ESLint into an existing Typescript codebase.在将 ESLint 引入现有的 Typescript 代码库后,我遇到了一些解析错误。

I have fixed most of the lint errors but babel-eslint as a parser throws quite a few errors like this:我已经修复了大部分 lint 错误,但是babel-eslint作为解析器会抛出很多这样的错误:

  23:32  error  Parsing error: Unexpected token, expected ","

  21 |       return state.set(
  22 |         'callsInProgress',
> 23 |         (state.callsInProgress as any).filter(i => i !== action.payload)
     |                                ^
  24 |       );
  25 |     }
  26 |     case actions.API_RESET: {

I assume this is because babel does not understand the typecasting as any .我认为这是因为babel不理解类型转换as any

How do i get this through the parser?我如何通过解析器得到这个?

My babel config is as follows:我的 babel 配置如下:

module.exports = {
  presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript'],
  plugins: ['@babel/plugin-proposal-class-properties', '@babel/plugin-transform-runtime', '@babel/plugin-transform-typescript']
};

Having a project that is using babel , eslint and typescript myself.自己有一个使用babeleslinttypescript的项目。

I recommend you to stop using eslint-babel and use @typescript-eslint instead/我建议你停止使用eslint-babel并改用@typescript-eslint /

typescript-eslint is a project that has been started by the developpers of Tslint (now deprecated) . typescript-eslint是一个由 Tslint (现已弃用)的开发者启动的项目。 It lint typescript code perfectly.它完美地去除了 typescript 代码。

Here is an example of my eslint installed npm packages:这是我的 eslint 安装的 npm 包的示例:

"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"eslint": "^5.16.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jsx-a11y": "^6.3.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-react": "^7.20.0",

Example of my .eslintrc :我的.eslintrc

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',

  plugins: [
    '@typescript-eslint',
    'eslint-plugin-node',
  ],

  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
  ],

  parserOptions: {
    "ecmaVersion": 2020
  },

  rules: {
    "comma-dangle": ["error", {
      "arrays": "always-multiline",
      "objects": "always-multiline",
      "imports": "always-multiline",
      "exports": "always-multiline",
      "functions": "always-multiline",
    }],
  },

  env: {
    es6: true,
    browser: true,
    node: true,
  },

  parserOptions: {
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },

  globals: {
    "global": false,
    "Promise": false,
  },
};

NOTE : I don't know if eslint-babel could be compatible with @typescript-eslint , maybe it does and you can use both.注意:我不知道eslint-babel是否可以与@typescript-eslint兼容,也许可以,您可以同时使用两者。

I finally agree with the accepted answer, after experienced the upgrade from JavaScript to TypeScript.在经历了从 JavaScript 升级到 TypeScript 之后,我终于同意接受的答案。 Post this answer to provide some additional key point.发布此答案以提供一些额外的关键点。

  1. @babel/eslint-parser (previously babel-eslint) can parse TS and let ESLint to work on TS. @babel/eslint-parser(以前的 babel-eslint)可以解析 TS 并让 ESLint 在 TS 上工作。

@babel/eslint-parser is a parser that allows ESLint to run on source code that is transformed by Babel. @babel/eslint-parser 是一个解析器,它允许 ESLint 在由 Babel 转换的源代码上运行。

  1. @babel/eslint-parser can't and won't be able to check type issue on TypeScript @babel/eslint-parser 不能也不能检查 TypeScript 上的类型问题

since @typescript-eslint uses TypeScript under the hood, its rules can be made type-aware, which is something Babel doesn't have the ability to do.因为@typescript-eslint 在底层使用了 TypeScript,所以它的规则可以是类型感知的,这是 Babel 无法做到的。 This I think is a must-have feature on TS.这个我觉得是TS必备的功能。

See doc: https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser#typescript见文档: https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser#typescript

So basically, if your project is a pure TS project, just use @typescript-eslint所以基本上,如果你的项目是纯 TS 项目,只需使用@typescript-eslint

{
  "plugins": ["@typescript-eslint"],
  "parser": "@typescript-eslint/parser",
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended"
  ]
}

If your project has both TS and JS, you can use @babel/eslint-parser as well如果你的项目既有 TS 又有 JS,你也可以使用@babel/eslint-parser

{
  "parser": "@babel/eslint-parser",
  "extends": ["airbnb", "plugin:prettier/recommended"],
  "env": {
    "browser": true,
    "commonjs": true,
    "es6": true,
    "jest": true
  },
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  },
  "rules": {
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "jsx": "never",
        "ts": "never",
        "tsx": "never"
      }
    ]
  },
  "overrides": [
    {
      "files": ["*.{ts,tsx}"],
      "parser": "@typescript-eslint/parser",
      "plugins": ["@typescript-eslint"],
      "extends": ["plugin:@typescript-eslint/recommended"]
    }
  ]
}

Just understand that using @babel/eslint-parser instead of the default parser esprima is because babel can lint on some experimental features (not much)只需了解使用@babel/eslint-parser而不是默认解析器esprima是因为 babel 可以对一些实验性功能进行 lint(不多)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预计打字稿 eslint 解析错误“]” - typescript eslint parsing error "]" expected 带有 ESLint 的 TypeScript:解析错误:关键字“enum”是保留的 eslint - TypeScript with ESLint: Parsing error: The keyword 'enum' is reserved eslint 使用 babel 模块解析器和 typescript 时的 ESLint 问题 - ESLint issue when using babel module-resolver and typescript ESlint 在 TypeScript 转换运算符“as”上解析错误“意外标记” - ESlint parsing error "Unexpected token" on TypeScript cast operator "as" typescript 中的 eslint 解析错误,lint 没有得到 parserOptions 配置 - Parsing error with eslint in typescript, lint doesnt get the parserOptions config 条件类型。 来自 TypeScript eslint 的解析错误 - Conditional Types. Parsing error from TypeScript eslint React typescript eslint 错误解析错误:意外的令牌? 如何解决这个问题? - React typescript eslint error Parsing error: Unexpected token ! How to fix this? 如何在 TypeScript 中的 document.getElementById() 方法类型转换期间处理 ESLint 错误? - How to handle ESLint error during type casting of document.getElementById() method in TypeScript? 解析错误:“parserOptions.project”已为 @typescript-eslint/parser 设置为 gatsby/typescript - Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser for gatsby/typescript 打字稿转换 - Typescript casting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM