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

Dav*_*ord 44 reactjs create-react-app

是否可以将TypeScript与create-react-app一起使用?如果是这样,你会怎么做?

小智 49

你可以这样做:

create-react-app my-app --scripts-version=react-scripts-ts
Run Code Online (Sandbox Code Playgroud)

请参阅https://github.com/wmonk/create-react-app-typescript

不需要弹出.

  • 现在,这似乎得到了微软的正式支持:https://github.com/Microsoft/TypeScript-React-Starter (9认同)

Tan*_*ong 19

Create React App v2.1.0现在支持TypeScript,因此您不再需要弹出或使用react-scripts-tsfork;

您可以将TypeScript添加到现有应用程序:

# update create-react-app first
npm update -g create-react-app 
create-react-app my-app --typescript
Run Code Online (Sandbox Code Playgroud)

然后将.js文件重命名为.tsx文件以开始使用TypeScript.

或者使用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  
Run Code Online (Sandbox Code Playgroud)

请注意,运行应用程序时将为您创建tsconfig.json.除了一些选项,您将能够编辑tsconfig.json .

积分:

[1] 文森特回答

[2] 迁移创建反应应用程序打字稿帖子


Mar*_*tin 7

在create-react-app 2.1版本中,Typescript现已开箱即用!

您现在可以使用它,如下所示(如此处所示): npx create-react-app@next app-name --scripts-version=2.0.6-next.c662dfb0 --typescript