You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.0 KiB

---
id: static-type-checking
title: Static Type Checking
permalink: docs/static-type-checking.html
prev: typechecking-with-prototypes.html
next: refs-and-the-dom.html
---
7 years ago
Static type checkers like [Flow](https://flowtype.org/) and [TypeScript](https://www.typescriptlang.org/) identify certain types of problems before you even run your code. They can also improve developer workflow by adding features like auto-completion. For this reason, we recommend using Flow or TypeScript instead of `PropTypes` for larger code bases.
7 years ago
## Flow
7 years ago
Below are instructions to add Flow to your React application. (You can learn more about using Flow with React [here](https://flow.org/en/docs/react/).)
7 years ago
### Using Flow with Babel
7 years ago
First install Babel. If you have not already done this, here is a [helpful setup guide](http://babeljs.io/docs/setup/).
7 years ago
Next install `babel-preset-flow` with either [Yarn](https://yarnpkg.com/) or [npm](https://www.npmjs.com/).
```bash
yarn add --dev babel-preset-flow
# or
7 years ago
npm install --save-dev babel-preset-flow
```
Then add `flow` to your Babel presets config.
7 years ago
```json
{
"presets": ["flow"]
}
```
7 years ago
### Using Flow with Create React App
7 years ago
[Create React App](https://github.com/facebookincubator/create-react-app) supports Flow by default. Just [install Flow](https://flow.org/en/docs/install/) and create a `.flowconfig` file by running `flow init`.
```bash
7 years ago
create-react-app my-app
cd my-app
yarn add --dev flow-bin
yarn run flow init
```
7 years ago
Flow will now be run as part of `create-react-app`'s scripts.
7 years ago
## TypeScript
7 years ago
You can learn more about using TypeScript with React [here](https://github.com/Microsoft/TypeScript-React-Starter#typescript-react-starter).
7 years ago
### Using TypeScript with Create React App
7 years ago
[react-scripts-ts](https://www.npmjs.com/package/react-scripts-ts) automatically configures a `create-react-app` project to support TypeScript. You can use it like this:
7 years ago
```bash
create-react-app my-app --scripts-version=react-scripts-ts
```
You can also try [typescript-react-starter](https://github.com/Microsoft/TypeScript-React-Starter#typescript-react-starter).