網站搜尋

typescript - 除非提供 '--jsx' 標誌,否則無法使用 JSX,當我執行 yarn start 時


我做了

  • 我執行了 yarn start,http://localhost:3000/ 成功了,但出現了很多問題

$ yarn start 同時 npm run start yarn run v1.22.4 $ react-scripts start

The following changes are being made to your tsconfig.json file:
  - compilerOptions.jsx must be react-jsx (to support the new JSX transform in React 17)

i 「wds」: Project is running at http://192.168.56.1/
i 「wds」: webpack output is served from
i 「wds」: Content not from webpack is served from C:\Users\taiga\Github\Typescript\eslint-app\example\public
i 「wds」: 404s will fallback to /
Starting the development server...
Compiled successfully!

You can now view example in the browser.

  Local:            http://localhost:3000
  On Your Network:  http://192.168.56.1:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

錯誤

  • Cannot use JSX unless the '--jsx' flag is provided
  •  Module './App' was resolved to 'c:/Users/taiga/Github/Typescript/eslint-app/example/src/App.tsx', but '--jsx' is not set.
  • Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'.

我嘗試

我試過這個

這是我的倉庫

我的目標

  • 對 react-typescript-app 使用 lint

答案#1

  • 在用 CRA(create react app)初始化專案之後,它能立即編譯/正常運作嗎?

可能是你更新的套件,試著在使用 CRA 初始化後不更新任何套件來編譯它。 (https://github.com/taiga-programming/example-typescript/commit/b7074af02df6ce469b867a69625d5c1de7a209eb)

但首先,你在提交 https://github.com/taiga-programming/example-typescript/commit/88ee7bfb164d9017883ea91535fadeb7e17691aa 的 tsconfig.json 中有些可疑之處:

   - "jsx": "react-jsx"
   + "jsx": "react"

嘗試把 "jsx": "react-jsx" 放回去。

答案#2

這是 create-react-app (CRA) 腳本的一個未解決問題。對我有效的唯一解決方案是在 .env 文件(在專案根目錄)中添加以下配置。

DISABLE_NEW_JSX_TRANSFORM=true