簡體   English   中英

create-react-app with --template typescript 不創建.tsx 文件

[英]create-react-app with -–template typescript not creating .tsx files

我決定將 React JS 與 TypeScript 一起使用。做一個簡單的 hello world 項目。 參考兩本書,他們都建議以下內容。 全新的項目,沒有升級或任何這些場景。 新鮮開始。

npx create-react-app try-react -–template typescript

那很好用。

但是,這些文件仍然具有 .js 擴展名。 書中,其中一個有截圖,已經是with.tsx擴展名了。

我看過這個鏈接https://create-react-app.dev/docs/adding-typescript/ 它說,

If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global remove create-react-app to ensure that npx always uses the latest version.

Global installs of create-react-app are no longer supported.

所以,我刪除了全局安裝。 再次嘗試。

一樣。 沒有擴展名為 .tsx 的文件。 本書作者沒有提及必須手動重命名這些文件。

所以,我的問題是,

  1. 模板不應該自動將所有擴展名設置為 .tsx 嗎?
  2. 執行此操作的唯一方法是手動將 from.js 更改為 .tsx 嗎?
  3. 我們是否總是這樣做,這確實是常態。 也許,這本書的作者確實手動完成了但忘記將其包含在說明中?

我有同樣的問題。 什么為我解決了它:

npm install cra-template-typescript

由於某種原因,模板不適合我。

就我而言,解決的問題是全局安裝 typescript 模板

npm install -g cra-template-typescript

一旦我這樣做了,我就跑了

npx create-react-app client-app --template typescript

Ant 它工作。 .tsx所在的所有腳本文件

提供獨立答案,因為我沒有評論或編輯@ChrisPabst 答案的聲譽。

我也在使用 David Choi 的書,並且出於與父母相同的原因偶然發現了這個問題......我無法獲得 --template typescript 來生成 typescript 文件。 ChrisPabst 的回答讓我更仔細地研究了從書中粘貼的命令與直接輸入的命令之間的區別,果然,這是第 5 章的粘貼:

npx create-react-app class-components -–template typescript

放大模板連字符,在此處重新粘貼:

-–

現在我直接輸入連字符:

--

現在讓我們重新粘貼問題文本本身的連字符:

-–

它絕對看起來像第一組,而不是第二組。

編輯:如果您獲得上述粘貼字符的 ASCII 值(例如,啟動 python 解釋器,然后為每個字符鍵入ord("–") ,粘貼的文本將為您提供

45 和 8211

自己輸入時是 45 和 45。)

tl;dr:如果您使用的是 David Choi 的書“全棧 React、TypeScript 和 Node”,那么上面關於cra-react-template的所有建議都是完全的紅鯡魚。 粘貼命令,但在按下回車鍵之前,將 go 返回並自己輸入雙連字符。 將正確生成 typescript 模板。

為了完整起見,我相信有問題的書是 David Choi 的 Full-Stack React、TypeScript 和 Node。 我也在從這本書中學習 React,我也遇到了這個問題。 這個問題實際上不在環境中,而是在命令本身(您可以在原始問題中看到)。

如果你正在在線閱讀這本書,並復制命令來設置一個新的 React 應用程序,你會得到

npx create-react-app hooks-components –template typescript

但是由於某種原因,這本書有一個非 ascii 破折號而不是正確的-- 所以模板 arg 被忽略。

我一直缺少的是這個錯誤。

npm ERR, arg Argument starts with non-ascii dash: this is probably invalid: –template

所以是的,它應該可以工作; 不,你不必做任何額外的事情。

你需要嘗試兩件事:

npm uninstall -g create-react-app

或者

yarn global remove create-react-app

這應該從今天開始工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM