首页 > 解决方案 > 在 create-react-app --template typescript 之后 TypeScript React 应用程序未呈现

问题描述

我使用npx create-react-app my-app --use-npm --template typescript.

启动开发服务器 ( cd my-app npm start) 后,代码编译成功,但是当浏览器窗口打开时,什么都没有呈现。

控制台说:工人注册成功 http://localhost:3000/(但仍然没有渲染)

但有时也:未捕获的语法错误:预期的表达式,得到 '<'

我通常对 JavaScript 没有任何问题create-react-app,但是对于 TypeScript,这会导致问题。我尝试使用不同版本的节点(14.0.0, 15.0.0, 15.0.1)(npm 版本7.0.2)和其他命令(npx create-react-app my-app --use-npm --typescript(可能相同))但这没有帮助。

生成的.tsconfig文件:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

我的package.json文件中生成的依赖项:

"dependencies": {
  "@testing-library/jest-dom": "^5.11.9",
  "@testing-library/react": "^11.2.5",
  "@testing-library/user-event": "^12.6.3",
  "@types/jest": "^26.0.20",
  "@types/node": "^12.19.16",
  "@types/react": "^17.0.1",
  "@types/react-dom": "^17.0.0",
  "react": "^17.0.1",
  "react-dom": "^17.0.1",
  "react-scripts": "4.0.2",
  "typescript": "^4.1.5",
  "web-vitals": "^1.1.0"
},

标签: javascriptreactjstypescriptnpmcreate-react-app

解决方案


推荐阅读