【问题标题】:React Project not compiling after adding TypeScript添加 TypeScript 后 React Project 未编译
【发布时间】:2020-12-12 22:43:38
【问题描述】:

我遇到了一个奇怪的错误

./src/index.js 第 56:15 行:解析错误:'>' 预期

我按照create-react-app 的官方指南将 TypeScript 添加到我现有的 Create React App 项目中。仅将两个 .js 文件(index 和 App)更改为 .tsx 后,项目编译失败。

我按照这个troubleshooting guide卸载了全局create-react-app

index.tsx 文件

import React from "react";
import ReactDOM from "react-dom";
ReactDOM.render(
  <App store={store} persistor={persistor} basename={PUBLIC_URL} />,
  document.getElementById("root")
);

注意:我已将 index.js 文件更改为 index.tsx,但 index.js 仍然出错。我猜它在编译为 .js 后会抛出错误

【问题讨论】:

  • 添加你的 App.tsx 和 Index.tsx?
  • 请附上相关行的内容(./src/index.js Line 56)

标签: reactjs typescript create-react-app


【解决方案1】:

经过 3 个小时的无用挣扎,我找到了错误的根本原因 :)

正如我之前在问题中提到的,我删除了全局 create-react-app 包以避免出现问题,但这造成了问题。我不知道为什么,但是当我重新安装了 create-react-app,问题就解决了!

【讨论】:

    【解决方案2】:

    /src/index.js Line 56:15: Parsing error: '&gt;' expected

    您在 .js 文件中使用 JSX。将文件重命名为.jsx(或者更好的`.tsx)。

    更多

    您可以通过 npx create-react-app example --template typescript ? 与 TypeScript 应用程序进行比较

    【讨论】:

    • 我已经将 index.js 更改为 index.tsx 并将 App.js 更改为 App.tsx
    • 如果您仍然在src/index.js 中收到错误,即使该文件已不存在,只需关闭并重新启动出现错误的进程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 2023-04-04
    • 2018-04-10
    • 1970-01-01
    • 2016-05-19
    • 2019-06-17
    相关资源
    最近更新 更多