简体   繁体   English

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

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

I decided to use React JS with TypeScript. Doing a simple hello world project.我决定将 React JS 与 TypeScript 一起使用。做一个简单的 hello world 项目。 Referring two books, and they both suggest the following.参考两本书,他们都建议以下内容。 Brand new project, not upgrading or any of those scenarios.全新的项目,没有升级或任何这些场景。 Starting fresh.新鲜开始。

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

That works fine.那很好用。

But, the files, they still have the.js extension.但是,这些文件仍然具有 .js 扩展名。 In the book, one of them has a screenshot, it is already with.tsx extension.书中,其中一个有截图,已经是with.tsx扩展名了。

I have looked at this link, https://create-react-app.dev/docs/adding-typescript/ .我看过这个链接https://create-react-app.dev/docs/adding-typescript/ It says,它说,

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.

So, I removed the global install.所以,我删除了全局安装。 tried again.再次尝试。

Same thing.一样。 no files with.tsx extension.没有扩展名为 .tsx 的文件。 The book author does not mention anything about having to rename these files manually.本书作者没有提及必须手动重命名这些文件。

So, my question is,所以,我的问题是,

  1. is the template not supposed to automatically set all extensions to.tsx?模板不应该自动将所有扩展名设置为 .tsx 吗?
  2. is the only way to do this is to manually change from.js to.tsx?执行此操作的唯一方法是手动将 from.js 更改为 .tsx 吗?
  3. do we always do this, and that is indeed the norm.我们是否总是这样做,这确实是常态。 Perhaps, the book author did do it manually but forgot to include it in the instructions?也许,这本书的作者确实手动完成了但忘记将其包含在说明中?

I had the same problem.我有同样的问题。 What solved it for me:什么为我解决了它:

npm install cra-template-typescript

For some reason the template was not there for me.由于某种原因,模板不适合我。

In my case what solved it was installing the typescript template globally就我而言,解决的问题是全局安装 typescript 模板

npm install -g cra-template-typescript

Once I did that, I ran一旦我这样做了,我就跑了

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

Ant it worked. Ant 它工作。 All script files where .tsx .tsx所在的所有脚本文件

Providing an independent answer because I don't have the reputation to comment on or edit @ChrisPabst's answer.提供独立答案,因为我没有评论或编辑@ChrisPabst 答案的声誉。

I'm also using the David Choi book, and stumbled across this question for the same reason as the parent...I couldn't get --template typescript to generate typescript files.我也在使用 David Choi 的书,并且出于与父母相同的原因偶然发现了这个问题......我无法获得 --template typescript 来生成 typescript 文件。 ChrisPabst's answer got me to look more closely at the difference between commands pasted from the book, and commands directly entered, and sure enough, here's a paste from Chapter 5: ChrisPabst 的回答让我更仔细地研究了从书中粘贴的命令与直接输入的命令之间的区别,果然,这是第 5 章的粘贴:

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

Zoom in on the template hyphens, re-pasted here:放大模板连字符,在此处重新粘贴:

-–

Now I type in the hyphens directly:现在我直接输入连字符:

--

Now let's re-paste the hyphens from the question text itself:现在让我们重新粘贴问题文本本身的连字符:

-–

It definitely looks like the first set, not the second.它绝对看起来像第一组,而不是第二组。

EDIT: If you get the ASCII values of the above pasted characters (eg fire up a python interpreter, and then type ord("–") for each character, the pasted text will give you编辑:如果您获得上述粘贴字符的 ASCII 值(例如,启动 python 解释器,然后为每个字符键入ord("–") ,粘贴的文本将为您提供

45 and 8211 45 和 8211

while entering it yourself is 45 and 45.)自己输入时是 45 和 45。)

tl;dr: If you're using the David Choi book "Full-Stack React, TypeScript, and Node", all of the advice above about cra-react-template is a complete red herring. tl;dr:如果您使用的是 David Choi 的书“全栈 React、TypeScript 和 Node”,那么上面关于cra-react-template的所有建议都是完全的红鲱鱼。 Paste the command, but before you press enter, go back and type in the double-hyphens yourself.粘贴命令,但在按下回车键之前,将 go 返回并自己输入双连字符。 A typescript template will be generated correctly.将正确生成 typescript 模板。

For completeness, I believe the book in question is Full-Stack React, TypeScript, and Node by David Choi.为了完整起见,我相信有问题的书是 David Choi 的 Full-Stack React、TypeScript 和 Node。 I too am learning React from this book, and I also ran into this problem.我也在从这本书中学习 React,我也遇到了这个问题。 This issue was not actually in the environment, but in the command itself (which you can see in the original question).这个问题实际上不在环境中,而是在命令本身(您可以在原始问题中看到)。

If you are reading the book online, and copy the command to set up a new React app, you get如果你正在在线阅读这本书,并复制命令来设置一个新的 React 应用程序,你会得到

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

But the book for some reason has a non-ascii dash instead of the correct -- .但是由于某种原因,这本书有一个非 ascii 破折号而不是正确的-- So the template arg is ignored.所以模板 arg 被忽略。

What I kept missing was this error.我一直缺少的是这个错误。

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

So yes it is supposed to work;所以是的,它应该可以工作; No you don't have to do anything extra.不,你不必做任何额外的事情。

You need to try two things:你需要尝试两件事:

npm uninstall -g create-react-app

or或者

yarn global remove create-react-app

This should work as of today.这应该从今天开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM