使用打字稿模板将 create-react-app 更新到 4.0 时出错

2023-12-23

我要更新react-scripts到下一个版本(4.0.0)这样我就可以使用本指南使用快速刷新功能here https://gist.github.com/iansu/282dbe3d722bd7231fa3224c0f403fa1。但是当重新启动服务器时,脚本不起作用,因为出现以下错误:

$ yarn start
yarn run v1.22.4
$ react-scripts start
E:\Github\Web\so-rank\node_modules\react-scripts\scripts\utils\verifyTypeScriptSetup.js:210
        appTsConfig.compilerOptions[option] = suggested;
                                            ^

TypeError: Cannot add property noFallthroughCasesInSwitch, object is not extensible
    at verifyTypeScriptSetup (E:\Github\Web\so-rank\node_modules\react-scripts\scripts\utils\verifyTypeScriptSetup.js:210:45)
    at Object.<anonymous> (E:\Github\Web\so-rank\node_modules\react-scripts\scripts\start.js:31:1)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
error Command failed with exit code 1.

这可以通过启用来解决noFallthroughCasesInSwitch选项在你的tsconfig.json。查看讨论here https://github.com/facebook/create-react-app/issues/9429#issuecomment-674994709了解更多信息。

{
  "compilerOptions": {
    "noFallthroughCasesInSwitch": true,
    ...
  },
  ...
}

对于任何好奇的人来说,上述解决方案并没有修复该错误。它只是跳过运行下面的错误代码,如果未提供,该代码会将建议值分配给打字稿编译器选项。这tsconfig.json产生于react-scripts默认情况下没有noFallthroughCasesInSwitch选项。添加该选项无需运行代码。

// Some options when not present in the tsconfig.json will be assigned
// a suggested value which crashes the program
if (suggested != null) {
  if (parsedCompilerOptions[option] === undefined) {
    appTsConfig.compilerOptions[option] = suggested; // error here
    ...
  }
} 

EDIT:

如果脚本因其他选项而崩溃,并且您的堆栈跟踪与我的问题中的堆栈跟踪相同,则您应该检查您的代码中是否缺少以下编译器选项tsconfig.json

These https://github.com/facebook/create-react-app/blob/ed958938f642007645dd5ac3466db36202f8754e/packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js#L116-L160是 Typescript 编译器选项中未指定时的建议值tsconfig.json

const compilerOptions = {
  // These are suggested values and will be set when not present in the
  // tsconfig.json
  target: {
    parsedValue: ts.ScriptTarget.ES5,
    suggested: 'es5',
  },
  lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
  allowJs: { suggested: true },
  skipLibCheck: { suggested: true },
  esModuleInterop: { suggested: true },
  allowSyntheticDefaultImports: { suggested: true },
  strict: { suggested: true },
  forceConsistentCasingInFileNames: { suggested: true },
  noFallthroughCasesInSwitch: { suggested: true },
  module: {
    parsedValue: ts.ModuleKind.ESNext,
    value: 'esnext',
    reason: 'for import() and import/export',
  },
  moduleResolution: {
    parsedValue: ts.ModuleResolutionKind.NodeJs,
    value: 'node',
    reason: 'to match webpack resolution',
  },
  resolveJsonModule: { value: true, reason: 'to match webpack loader' },
  isolatedModules: { value: true, reason: 'implementation limitation' },
  noEmit: { value: true },
  jsx: {
    parsedValue: ts.JsxEmit.React,
    suggested: 'react',
  },
  paths: { value: undefined, reason: 'aliased imports are not supported' },
};

您需要将这些选项显式添加到您的tsconfig.json因此脚本可以跳过有问题的分支并避免崩溃。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用打字稿模板将 create-react-app 更新到 4.0 时出错 的相关文章

  • Phonegap应用程序与reactjs [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 有人用reactjs 通过facebook 编写了phonegap应用程序吗 任何指向参考实现的指针将不胜感激 此外 您建议将什么数据
  • ReactJS 模式无法使用 Materialise css 打开

    我是 ReactJS 的新手 正在学习使用 Materialise css 创建模型 https materializecss com https materializecss com import React Component from
  • 渲染没有返回任何内容。这通常意味着缺少 return 语句。或者,不渲染任何内容,返回 null [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我在 React 中有一个组件 我正在将其导入到 index js 中 但它给出了以下错误 渲染没有返回任何内容 这通常意味着缺少
  • 修改对象实例的 TypeScript 类装饰器

    我正在为 Aurelia 制作一个插件 需要一个类装饰器 将属性添加到新对象实例 并且 使用新对象作为参数调用外部函数 我已经查看了示例 到目前为止我已经将它们放在一起 伪 代码 return function addAndCall tar
  • 如何为 Apollo 的 React HOC 定义 props 接口?

    我正在尝试使用 Apollo 的 React HOC 来获取数据并将其传递给我的组件 但出现以下错误 Argument of type typeof BrandList is not assignable to parameter of t
  • 需要将焦点放在react中的div上

    我有一个之前用 H1 标签包裹的文本 页面加载后 我需要专注于该文本 为了方便起见 我将它包装在 div 中 render const translate billing primaryContactSelection true this
  • 使用 System.js 导入 Typescript 编译的模块时出错

    我最近正在学习使用 system js 导入由 Typescript 编译的模块 这些模块之前是为 require js 编译的 并且工作正常 但是合并到system js时 应用时无法导入模块系统生产 js 控制台说 Uncaught i
  • 浏览器列表更新没有任何作用

    In our 创建反应应用程序 https github com facebook create react app the node modules caniuse lite package json版本为 1 0 30001251 ya
  • TypeScript 中 C# 类虚拟成员的等效项

    因此 在 C 中 当我创建模型类和延迟加载内容时 我会执行以下操作 public int User ID get set public int Dept ID get set 然后在我的班级稍远一点的地方 我像这样弹出我的虚拟 public
  • React Router LINK不传递参数

    我有一个 React 有声读物搜索应用程序 它按作者姓氏从 Librivox 获取数据并显示书籍列表 列表中的每个项目都有一个链接 该链接依次转到显示的单独的完整图书数据组件 该特定书籍 或用户从列表中选择的任何书籍 的所有数据 在我的一生
  • 未捕获的类型错误:(0,_sanity_client__WEBPACK_IMPORTED_MODULE_0__.sanityClient)不是函数

    我第一次尝试从 Sanity 获取数据到我的 React 应用程序中 目前 我正在尝试一些小事情 所以我有一个名为website images 其图像类型称为about和一个单独的字符串alt 在我正在观看的教程视频中 她说她正在使用 GR
  • 在反应中循环图像的最佳方法

    我已经使用 create react app 创建了一个项目 并且在源代码中有一个名为 images 的文件夹 我想在图像文件夹内循环并显示它们 到目前为止我的代码如下所示 import React Component from react
  • Typescript 项目引用:处理引用项目的第三方依赖项

    我有一个包含 3 个目录的项目client api and shared The shared目录包含 typescript 类型和定义engine我想与之共享的文件夹client and api 此外 shared目录还需要一些第三方依赖
  • 如何正确使用Hooks?

    我有一些问题 1 我定义了一个从API获取数据并调用它的函数useEffect 效果很好 但我在 VScode 中收到此警告 React Hook React useEffect 缺少依赖项 getOpenOrders 包含它或删除依赖项数
  • getFieldValue 或 Formik 中的类似内容

    有没有办法获取 formik 中单击处理程序内字段的值 您可以使用setFieldValue在那里 所以我假设 但在任何地方都找不到 Formik 应该有类似的东西来检索值
  • Formik 和 Yup:类型错误:无法读取未定义的属性“对象”

    我是 React 新手 正在尝试使用 formik 和 yup 进行验证 我目前收到以下错误 类型错误 无法读取未定义的属性 对象 用这个代码 validationSchema Yup object shape firstName Yup
  • Angular 2延迟加载模块-服务不是单例

    我已经在我的应用程序中实现了延迟加载模块 app module ts 配置正确 NgModule declarations AppComponent HeaderComponent HomeComponent imports Browser
  • Material UI 的自定义 ESLint 导入规则

    我在 React 中有一个项目 使用 Material UI 我正在应用其中之一他们建议的方法 https material ui com guides minimizing bundle size option 2减少我的包大小 基本上
  • Angular 中的动态子组件

    我正在构建一个具有一致的元素列表设计模式的应用程序 如果我有一个 A 类型的对象 我会创建AComponent它接受a作为输入 然后创建另一个组件来迭代 A 列表 AListComponent 那么如果我有一个对象 B 我需要做同样的事情
  • 使用验证器禁用 FormGroup

    I had formArray我的复选框checkboxForm 如果没有复选框 我需要禁用按钮提交checked 我在我的上实现了自定义验证器checkboxForm 这是我尝试过的 Ts file get formReceivedSum

随机推荐