简体   繁体   English

create-react-app 可以与 TypeScript 一起使用吗

[英]Can create-react-app be used with TypeScript

Is it possible to use TypeScript with create-react-app ?是否可以将 TypeScript 与create-react-app If so, how would you do it?如果是这样,你会怎么做?

You can do it like this:你可以这样做:

create-react-app my-app --scripts-version=react-scripts-ts

See https://github.com/wmonk/create-react-app-typescripthttps://github.com/wmonk/create-react-app-typescript

No ejecting required for this.这不需要弹出。

This is the way to go:这是要走的路:

# update create-react-app first
npm update -g create-react-app 
create-react-app my-app --typescript

Create React App v2.1.0 now supports TypeScript, so you no longer need to eject or use the react-scripts-ts fork; Create React App v2.1.0现在支持TypeScript,不再需要弹出或使用react-scripts-ts fork;

Or you can add TypeScript to the existing app :或者您可以将 TypeScript 添加到现有应用程序

yarn add typescript @types/node @types/react @types/react-dom @types/jest
# or
npm install --save typescript @types/node @types/react @types/react-dom @types/jest  

then rename .js files to .tsx files to start using TypeScript.然后将.js文件重命名为.tsx文件以开始使用 TypeScript。

Note that a tsconfig.json will be created for you when you run the app.请注意,当您运行应用程序时,会为您创建一个 tsconfig.json。 You will be able to edit tsconfig.json except for some options .除了某些选项之外,您将能够编辑 tsconfig.json。

Credits:学分:

[1] Vincent answer [1]文森特回答

[2] migrate create react app typescript post [2] migrate create react app typescript post

Typescript is available out of the box now, in create-react-app version 2.1! Typescript 现在开箱即用,在 create-react-app 版本 2.1 中!

You can use it now, like this ( as shown here ): npx create-react-app@next app-name --scripts-version=2.0.6-next.c662dfb0 --typescript您现在可以使用它,如下所示( 如下所示): npx create-react-app@next app-name --scripts-version=2.0.6-next.c662dfb0 --typescript

I posted a feature request in the github issues for create-react-app and here was the response:我在 github 问题中为 create-react-app 发布了一个功能请求,这是响应:

Hi!你好! We're not quite ready to consider TypeScript yet (see #142 for updates).我们还没有准备好考虑 TypeScript(有关更新,请参阅 #142)。 We have yet to complete our Flow integration (which we will suggest as first option for type safety).我们尚未完成 Flow 集成(我们建议将其作为类型安全的首选)。

Additionally, there's really interesting things going on right now.此外,现在正在发生一些非常有趣的事情。 See babel/babylon#320 and babel/babylon#444.参见 babel/babylon#320 和 babel/babylon#444。 TypeScript may soon be parsed by Babylon (and compiled by Babel). TypeScript 可能很快就会被 Babylon 解析(并由 Babel 编译)。 Microsoft has interest in seeing this happen, and are working on a WIP themselves, too. Microsoft 有兴趣看到这种情况发生,并且自己也在开发 WIP。

This consideration is probably still a few months out.这种考虑可能还需要几个月的时间。 Sorry!对不起!

Though unendorsed by us, there is a fork of CRA which supports TypeScript.虽然我们没有认可,但有一个支持 TypeScript 的 CRA 分支。 We cannot provide support if it doesn't work for you, but it should!如果它不适合您,我们无法提供支持,但它应该!

Not sure how to square that with levito's response.不知道如何将其与 Levito 的回应相提并论。

Here is the link to the github issue:这是github问题的链接:

https://github.com/facebookincubator/create-react-app/issues/1998#issuecomment-295311100 https://github.com/facebookincubator/create-react-app/issues/1998#issuecomment-295311100

Here is update about create-react-app --typescript flag这是关于create-react-app --typescript标志的更新

Previously in order to generate new project in typescript with create-react-app we used typescript flag like below以前为了使用create-react-app在打字稿中生成新项目,我们使用了如下typescript标志

npx create-react-app my-app --typescript

But this typescript flag will be removed in next major release of create-react-app as mentioned in the create-react-app --help但是这个typescript标志将在create-react-app下一个主要版本中删除,如create-react-app --help

 --typescript (this option will be removed in favor of templates in the next major release of create-react-app)

So the new solution will be to use a template as mentioned here所以新的解决方案是使用这里提到的模板

ex:前任:

npx create-react-app my-app --template typescript

Also, you can create your app using this starter ( https://github.com/vtereshyn/react-typescript-eslint-starter )此外,您可以使用此启动器( https://github.com/vtereshyn/react-typescript-eslint-starter ) 创建您的应用程序

It doesn't use create-react-app, so fully customizable and changeable.它不使用 create-react-app,所以完全可定制和多变。 Because using create-react-app it will be hard to add something in some cases...因为使用 create-react-app 在某些情况下很难添加一些东西......

You can use Facebook's Official Typescript Template for Create React App.你可以使用 Facebook 的官方Typescript 模板来创建 React App。

Do

npx create-react-app my-app --template typescript

or或者

yarn create react-app my-app --template typescript

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

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