简体   繁体   English

Typescript 在 create-react-app 中 EXTEND_ESLINT=true 时解析错误

[英]Typescript parsing error when EXTEND_ESLINT=true in create-react-app

Stripped down demonstration - GitHub精简演示 - GitHub

We currently have a create-react-app project that is undergoing an incremental migration from Flow to Typescript.我们目前有一个 create-react-app 项目,该项目正在进行从 Flow 到 Typescript 的增量迁移。 This meant disabling some undesirable ESLint rules.这意味着禁用一些不受欢迎的 ESLint 规则。 In order to customise ESLint, I've added EXTEND_ESLINT=true to the .env file.为了自定义 ESLint,我在.env文件中添加了EXTEND_ESLINT=true

Before adding this setting, my Typescript code compiles fine.在添加此设置之前,我的 Typescript 代码编译良好。 Afterwards, I get parsing errors on certain (but not all) Typescript grammars.之后,我在某些(但不是全部)Typescript 语法上遇到解析错误。

// Type guards
export function f0<T>(x: T|undefined): x is T { ...

// Constrained generics
export function f1<T extends number>(x: T) { ...

// Type assertions
... return x as T

There may be other unrecognised syntaxes I haven't found yet.我可能还没有找到其他无法识别的语法。

So far至今

Solutions解决方案

  • Ejecting is not an option.弹出不是一种选择。 If no other solution can be found, I would rather just do the Flow -> TS conversion in one go.如果找不到其他解决方案,我宁愿在一个 go 中进行 Flow -> TS 转换。

  • We currently extend our CRA configuration with customize-cra .我们目前使用customize-cra扩展我们的 CRA 配置。 Solutions involving this are welcome.欢迎涉及此问题的解决方案。

  • I enjoy the flexibility the .eslintrc gives me but I am happy to do away with it, provided I can still set lint rules.我喜欢.eslintrc给我的灵活性,但我很乐意取消它,前提是我仍然可以设置 lint 规则。

Notes笔记

  • I've included customize-cra in the demo repo to accurately reflect our project, but the problem persists without customize-cra , indicating that it is likely not the culprit.我在演示 repo 中包含了customize-cra以准确反映我们的项目,但是没有customize-cra问题仍然存在,这表明它可能不是罪魁祸首。

  • See src/components/TestComponent/fn.ts for examples of the problem syntax.有关问题语法的示例,请参见src/components/TestComponent/fn.ts

  • My current hypothesis is that there's some setup in the CRA ESLint config that doesn't get carried over when EXTEND_ESLINT=true .我目前的假设是 CRA ESLint 配置中有一些设置在EXTEND_ESLINT=true时不会被继承。

Updates更新

UPDATE: Update react-scripts to latest version at least 3.4.1.更新:将 react-scripts 更新到至少 3.4.1 的最新版本。 It's fixed.它是固定的。

For pre 3.4.1 versions of react-scripts,对于 3.4.1 之前的 react-scripts 版本,

Open node_modules/react-scripts/config/webpack.config.js打开node_modules/react-scripts/config/webpack.config.js

Replace code block from line 365 with this code.用此代码替换第 365 行的代码块。

  if (process.env.EXTEND_ESLINT === 'true') {
    return undefined
  } else {
    return {
      extends: [require.resolve('eslint-config-react-app')],
    };
  }
})(),
useEslintrc: process.env.EXTEND_ESLINT === 'true',

Now if you start your app by yarn or npm, you will see it is fixed.现在,如果您通过 yarn 或 npm 启动您的应用程序,您将看到它已修复。

The fix is originally introduced here该修复程序最初在此处介绍

https://github.com/facebook/create-react-app/issues/7776#issuecomment-567587642 https://github.com/facebook/create-react-app/issues/7776#issuecomment-567587642

Run npx patch-package react-scripts to make a patch for it.运行npx patch-package react-scripts为其制作补丁。 And add "postinstall": "patch-package" to your package.json scripts section.并将"postinstall": "patch-package"添加到 package.json 脚本部分。

The patch will be automatically applied after npm install npm install后会自动应用补丁

For anyone else encountering this issue, there is an open issue at CRA which seems to be the cause.对于遇到此问题的其他人,CRA 有一个未解决的问题,这似乎是原因。

暂无
暂无

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

相关问题 create-react-app — 如何将 EXTEND_ESLINT 设置为 true? - create-react-app — how to set EXTEND_ESLINT to true? 为什么 create-react-app 在尝试 eslint 时会给出 typescript 错误(尽管没有 typescript 文件)? - Why is create-react-app giving typescript error when trying to eslint (though no typescript files)? 如何扩展 Eslint 以使用 create-react-app - How to Extend Eslint to work with create-react-app 尝试在 create-react-app 项目中扩展 ESLint 的问题 - Issues trying to extend ESLint in a create-react-app project create-react-app eslint 错误“解析错误:‘import’和‘export’可能只出现在‘sourceType:module’中” - create-react-app eslint error "Parsing error: 'import' and 'export' may appear only with 'sourceType: module'" 使用打字稿模板将 create-react-app 更新到 4.0 时出错 - Error when updating create-react-app to 4.0 with typescript template 由于 ESLint 错误,我的 create-react-app 无法编译 - My create-react-app is failing to compile due to ESLint error Create-React-App:在本地机器上部署时出现 ESlint 错误 - Create-React-App: ESlint error while deployment on local machine Eslint 错误导致 create-react-app 编译失败 - Eslint error causing create-react-app failed to compile create-react-app 的开发服务器遇到 ESLINT 检测到的错误时停止自动刷新 - Development server of create-react-app stops auto refresh when meeting an error detected by ESLINT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM