Skip to content

Files

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Latest commit

10632fe · Apr 28, 2019

History

History
48 lines (29 loc) · 2.19 KB

adding-typescript.md

File metadata and controls

48 lines (29 loc) · 2.19 KB
id title
adding-typescript
Adding TypeScript

Note: this feature is available with react-scripts@2.1.0 and higher.

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

To start a new Create React App project with TypeScript, you can run:

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

# or

yarn create react-app my-app --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 to ensure that npx always uses the latest version.

To add TypeScript to a Create React App project, first install it:

npm install --save typescript @types/node @types/react @types/react-dom @types/jest

# or

yarn add typescript @types/node @types/react @types/react-dom @types/jest

Next, rename any file to be a TypeScript file (e.g. src/index.js to src/index.tsx) and restart your development server!

Type errors will show up in the same console as the build one.

To learn more about TypeScript, check out its documentation.

Note: If your project is not created with TypeScript enabled, npx may be using a cached version of create-react-app. Remove previously installed versions with npm uninstall -g create-react-app (see #6119).

Note: You are not required to make a tsconfig.json file, one will be made for you. You are allowed to edit the generated TypeScript configuration.

Note: If you are currently using create-react-app-typescript, see this blog post for instructions on how to migrate to Create React App.

Note: We recommend using VSCode for a better integrated experience.

Note: Constant enums and namespaces are not supported.