{"componentChunkName":"component---src-templates-docs-js","path":"/docs/static-type-checking.html","result":{"data":{"markdownRemark":{"html":"<p>Static type checkers like <a href=\"https://flow.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Flow</a> and <a href=\"https://www.typescriptlang.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">TypeScript</a> 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 <code class=\"gatsby-code-text\">PropTypes</code> for larger code bases.</p>\n<h2 id=\"flow\"><a href=\"#flow\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Flow </h2>\n<p><a href=\"https://flow.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Flow</a> is a static type checker for your JavaScript code. It is developed at Facebook and is often used with React. It lets you annotate the variables, functions, and React components with a special type syntax, and catch mistakes early. You can read an <a href=\"https://flow.org/en/docs/getting-started/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">introduction to Flow</a> to learn its basics.</p>\n<p>To use Flow, you need to:</p>\n<ul>\n<li>Add Flow to your project as a dependency.</li>\n<li>Ensure that Flow syntax is stripped from the compiled code.</li>\n<li>Add type annotations and run Flow to check them.</li>\n</ul>\n<p>We will explain these steps below in detail.</p>\n<h3 id=\"adding-flow-to-a-project\"><a href=\"#adding-flow-to-a-project\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Adding Flow to a Project </h3>\n<p>First, navigate to your project directory in the terminal. You will need to run the following command:</p>\n<p>If you use <a href=\"https://yarnpkg.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Yarn</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">yarn</span> <span class=\"token function\">add</span> --dev flow-bin</code></pre></div>\n<p>If you use <a href=\"https://www.npmjs.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">npm</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> --save-dev flow-bin</code></pre></div>\n<p>This command installs the latest version of Flow into your project.</p>\n<p>Now, add <code class=\"gatsby-code-text\">flow</code> to the <code class=\"gatsby-code-text\">&quot;scripts&quot;</code> section of your <code class=\"gatsby-code-text\">package.json</code> to be able to use this from the terminal:</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// ...</span>\n  <span class=\"token string\">\"scripts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n<span class=\"gatsby-highlight-code-line\">    <span class=\"token string\">\"flow\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"flow\"</span><span class=\"token punctuation\">,</span></span>    <span class=\"token comment\">// ...</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token comment\">// ...</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Finally, run one of the following commands:</p>\n<p>If you use <a href=\"https://yarnpkg.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Yarn</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">yarn</span> run flow init</code></pre></div>\n<p>If you use <a href=\"https://www.npmjs.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">npm</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">npm</span> run flow init</code></pre></div>\n<p>This command will create a Flow configuration file that you will need to commit.</p>\n<h3 id=\"stripping-flow-syntax-from-the-compiled-code\"><a href=\"#stripping-flow-syntax-from-the-compiled-code\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Stripping Flow Syntax from the Compiled Code </h3>\n<p>Flow extends the JavaScript language with a special syntax for type annotations. However, browsers aren’t aware of this syntax, so we need to make sure it doesn’t end up in the compiled JavaScript bundle that is sent to the browser.</p>\n<p>The exact way to do this depends on the tools you use to compile JavaScript.</p>\n<h4 id=\"create-react-app\"><a href=\"#create-react-app\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create React App </h4>\n<p>If your project was set up using <a href=\"https://github.com/facebookincubator/create-react-app\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Create React App</a>, congratulations! The Flow annotations are already being stripped by default so you don’t need to do anything else in this step.</p>\n<h4 id=\"babel\"><a href=\"#babel\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Babel </h4>\n<blockquote>\n<p>Note:</p>\n<p>These instructions are <em>not</em> for Create React App users. Even though Create React App uses Babel under the hood, it is already configured to understand Flow. Only follow this step if you <em>don’t</em> use Create React App.</p>\n</blockquote>\n<p>If you manually configured Babel for your project, you will need to install a special preset for Flow.</p>\n<p>If you use Yarn, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">yarn</span> <span class=\"token function\">add</span> --dev @babel/preset-flow</code></pre></div>\n<p>If you use npm, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> --save-dev @babel/preset-flow</code></pre></div>\n<p>Then add the <code class=\"gatsby-code-text\">flow</code> preset to your <a href=\"https://babeljs.io/docs/usage/babelrc/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Babel configuration</a>. For example, if you configure Babel through <code class=\"gatsby-code-text\">.babelrc</code> file, it could look like this:</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token punctuation\">{</span>\n  <span class=\"token string\">\"presets\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n<span class=\"gatsby-highlight-code-line\">    <span class=\"token string\">\"@babel/preset-flow\"</span><span class=\"token punctuation\">,</span></span>    <span class=\"token string\">\"react\"</span>\n  <span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This will let you use the Flow syntax in your code.</p>\n<blockquote>\n<p>Note:</p>\n<p>Flow does not require the <code class=\"gatsby-code-text\">react</code> preset, but they are often used together. Flow itself understands JSX syntax out of the box.</p>\n</blockquote>\n<h4 id=\"other-build-setups\"><a href=\"#other-build-setups\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Other Build Setups </h4>\n<p>If you don’t use either Create React App or Babel, you can use <a href=\"https://github.com/flowtype/flow-remove-types\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">flow-remove-types</a> to strip the type annotations.</p>\n<h3 id=\"running-flow\"><a href=\"#running-flow\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Running Flow </h3>\n<p>If you followed the instructions above, you should be able to run Flow for the first time.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">yarn</span> flow</code></pre></div>\n<p>If you use npm, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">npm</span> run flow</code></pre></div>\n<p>You should see a message like:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"gatsby-code-text\"><code class=\"gatsby-code-text\">No errors!\n✨  Done in 0.17s.</code></pre></div>\n<h3 id=\"adding-flow-type-annotations\"><a href=\"#adding-flow-type-annotations\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Adding Flow Type Annotations </h3>\n<p>By default, Flow only checks the files that include this annotation:</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token comment\">// @flow</span></code></pre></div>\n<p>Typically it is placed at the top of a file. Try adding it to some files in your project and run <code class=\"gatsby-code-text\">yarn flow</code> or <code class=\"gatsby-code-text\">npm run flow</code> to see if Flow already found any issues.</p>\n<p>There is also <a href=\"https://flow.org/en/docs/config/options/#toc-all-boolean\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">an option</a> to force Flow to check <em>all</em> files regardless of the annotation. This can be too noisy for existing projects, but is reasonable for a new project if you want to fully type it with Flow.</p>\n<p>Now you’re all set! We recommend to check out the following resources to learn more about Flow:</p>\n<ul>\n<li><a href=\"https://flow.org/en/docs/types/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Flow Documentation: Type Annotations</a></li>\n<li><a href=\"https://flow.org/en/docs/editors/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Flow Documentation: Editors</a></li>\n<li><a href=\"https://flow.org/en/docs/react/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Flow Documentation: React</a></li>\n<li><a href=\"https://medium.com/flow-type/linting-in-flow-7709d7a7e969\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Linting in Flow</a></li>\n</ul>\n<h2 id=\"typescript\"><a href=\"#typescript\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>TypeScript </h2>\n<p><a href=\"https://www.typescriptlang.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">TypeScript</a> is a programming language developed by Microsoft. It is a typed superset of JavaScript, and includes its own compiler. Being a typed language, TypeScript can catch errors and bugs at build time, long before your app goes live. You can learn more about using TypeScript with React <a href=\"https://github.com/Microsoft/TypeScript-React-Starter#typescript-react-starter\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">here</a>.</p>\n<p>To use TypeScript, you need to:</p>\n<ul>\n<li>Add TypeScript as a dependency to your project</li>\n<li>Configure the TypeScript compiler options</li>\n<li>Use the right file extensions</li>\n<li>Add definitions for libraries you use</li>\n</ul>\n<p>Let’s go over these in detail.</p>\n<h3 id=\"using-typescript-with-create-react-app\"><a href=\"#using-typescript-with-create-react-app\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using TypeScript with Create React App </h3>\n<p>Create React App supports TypeScript out of the box.</p>\n<p>To create a <strong>new project</strong> with TypeScript support, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\">npx create-react-app my-app --template typescript</code></pre></div>\n<p>You can also add it to an <strong>existing Create React App project</strong>, <a href=\"https://facebook.github.io/create-react-app/docs/adding-typescript\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">as documented here</a>.</p>\n<blockquote>\n<p>Note:</p>\n<p>If you use Create React App, you can <strong>skip the rest of this page</strong>. It describes the manual setup which doesn’t apply to Create React App users.</p>\n</blockquote>\n<h3 id=\"adding-typescript-to-a-project\"><a href=\"#adding-typescript-to-a-project\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Adding TypeScript to a Project </h3>\n<p>It all begins with running one command in your terminal.</p>\n<p>If you use <a href=\"https://yarnpkg.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Yarn</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">yarn</span> <span class=\"token function\">add</span> --dev typescript</code></pre></div>\n<p>If you use <a href=\"https://www.npmjs.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">npm</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">npm</span> <span class=\"token function\">install</span> --save-dev typescript</code></pre></div>\n<p>Congrats! You’ve installed the latest version of TypeScript into your project. Installing TypeScript gives us access to the <code class=\"gatsby-code-text\">tsc</code> command. Before configuration, let’s add <code class=\"gatsby-code-text\">tsc</code> to the “scripts” section in our <code class=\"gatsby-code-text\">package.json</code>:</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// ...</span>\n  <span class=\"token string\">\"scripts\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n<span class=\"gatsby-highlight-code-line\">    <span class=\"token string\">\"build\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"tsc\"</span><span class=\"token punctuation\">,</span></span>    <span class=\"token comment\">// ...</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token comment\">// ...</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"configuring-the-typescript-compiler\"><a href=\"#configuring-the-typescript-compiler\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Configuring the TypeScript Compiler </h3>\n<p>The compiler is of no help to us until we tell it what to do. In TypeScript, these rules are defined in a special file called <code class=\"gatsby-code-text\">tsconfig.json</code>. To generate this file:</p>\n<p>If you use <a href=\"https://yarnpkg.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Yarn</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">yarn</span> run tsc --init</code></pre></div>\n<p>If you use <a href=\"https://www.npmjs.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">npm</a>, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\">npx tsc --init</code></pre></div>\n<p>Looking at the now generated <code class=\"gatsby-code-text\">tsconfig.json</code>, you can see that there are many options you can use to configure the compiler. For a detailed description of all the options, check <a href=\"https://www.typescriptlang.org/docs/handbook/tsconfig-json.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">here</a>.</p>\n<p>Of the many options, we’ll look at <code class=\"gatsby-code-text\">rootDir</code> and <code class=\"gatsby-code-text\">outDir</code>. In its true fashion, the compiler will take in typescript files and generate javascript files. However we don’t want to get confused with our source files and the generated output.</p>\n<p>We’ll address this in two steps:</p>\n<ul>\n<li>Firstly, let’s arrange our project structure like this. We’ll place all our source code in the <code class=\"gatsby-code-text\">src</code> directory.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"gatsby-code-text\"><code class=\"gatsby-code-text\">├── package.json\n├── src\n│   └── index.ts\n└── tsconfig.json</code></pre></div>\n<ul>\n<li>Next, we’ll tell the compiler where our source code is and where the output should go.</li>\n</ul>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"jsx\"><pre class=\"gatsby-code-jsx\"><code class=\"gatsby-code-jsx\"><span class=\"token comment\">// tsconfig.json</span>\n\n<span class=\"token punctuation\">{</span>\n  <span class=\"token string\">\"compilerOptions\"</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// ...</span>\n<span class=\"gatsby-highlight-code-line\">    <span class=\"token string\">\"rootDir\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"src\"</span><span class=\"token punctuation\">,</span></span><span class=\"gatsby-highlight-code-line\">    <span class=\"token string\">\"outDir\"</span><span class=\"token operator\">:</span> <span class=\"token string\">\"build\"</span></span>    <span class=\"token comment\">// ...</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Great! Now when we run our build script the compiler will output the generated javascript to the <code class=\"gatsby-code-text\">build</code> folder. The <a href=\"https://github.com/Microsoft/TypeScript-React-Starter/blob/master/tsconfig.json\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">TypeScript React Starter</a> provides a <code class=\"gatsby-code-text\">tsconfig.json</code> with a good set of rules to get you started.</p>\n<p>Generally, you don’t want to keep the generated javascript in your source control, so be sure to add the build folder to your <code class=\"gatsby-code-text\">.gitignore</code>.</p>\n<h3 id=\"file-extensions\"><a href=\"#file-extensions\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>File extensions </h3>\n<p>In React, you most likely write your components in a <code class=\"gatsby-code-text\">.js</code> file. In TypeScript we have 2 file extensions:</p>\n<p><code class=\"gatsby-code-text\">.ts</code> is the default file extension while <code class=\"gatsby-code-text\">.tsx</code> is a special extension used for files which contain <code class=\"gatsby-code-text\">JSX</code>.</p>\n<h3 id=\"running-typescript\"><a href=\"#running-typescript\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Running TypeScript </h3>\n<p>If you followed the instructions above, you should be able to run TypeScript for the first time.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">yarn</span> build</code></pre></div>\n<p>If you use npm, run:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token function\">npm</span> run build</code></pre></div>\n<p>If you see no output, it means that it completed successfully.</p>\n<h3 id=\"type-definitions\"><a href=\"#type-definitions\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Type Definitions </h3>\n<p>To be able to show errors and hints from other packages, the compiler relies on declaration files. A declaration file provides all the type information about a library. This enables us to use javascript libraries like those on npm in our project. </p>\n<p>There are two main ways to get declarations for a library:</p>\n<p><strong>Bundled</strong> - The library bundles its own declaration file. This is great for us, since all we need to do is install the library, and we can use it right away. To check if a library has bundled types, look for an <code class=\"gatsby-code-text\">index.d.ts</code> file in the project. Some libraries will have it specified in their <code class=\"gatsby-code-text\">package.json</code> under the <code class=\"gatsby-code-text\">typings</code> or <code class=\"gatsby-code-text\">types</code> field.</p>\n<p><strong><a href=\"https://github.com/DefinitelyTyped/DefinitelyTyped\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">DefinitelyTyped</a></strong> - DefinitelyTyped is a huge repository of declarations for libraries that don’t bundle a declaration file. The declarations are crowd-sourced and managed by Microsoft and open source contributors. React for example doesn’t bundle its own declaration file. Instead we can get it from DefinitelyTyped. To do so enter this command in your terminal.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"gatsby-code-bash\"><code class=\"gatsby-code-bash\"><span class=\"token comment\"># yarn</span>\n<span class=\"token function\">yarn</span> <span class=\"token function\">add</span> --dev @types/react\n\n<span class=\"token comment\"># npm</span>\n<span class=\"token function\">npm</span> i --save-dev @types/react</code></pre></div>\n<p><strong>Local Declarations</strong>\nSometimes the package that you want to use doesn’t bundle declarations nor is it available on DefinitelyTyped. In that case, we can have a local declaration file. To do this, create a <code class=\"gatsby-code-text\">declarations.d.ts</code> file in the root of your source directory. A simple declaration could look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"gatsby-code-typescript\"><code class=\"gatsby-code-typescript\"><span class=\"token keyword\">declare</span> <span class=\"token keyword\">module</span> <span class=\"token string\">'querystring'</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">export</span> <span class=\"token keyword\">function</span> <span class=\"token function\">stringify</span><span class=\"token punctuation\">(</span>val<span class=\"token operator\">:</span> object<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n  <span class=\"token keyword\">export</span> <span class=\"token keyword\">function</span> <span class=\"token function\">parse</span><span class=\"token punctuation\">(</span>val<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> object\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>You are now ready to code! We recommend to check out the following resources to learn more about TypeScript:</p>\n<ul>\n<li><a href=\"https://www.typescriptlang.org/docs/handbook/basic-types.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">TypeScript Documentation: Basic Types</a></li>\n<li><a href=\"https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">TypeScript Documentation: Migrating from Javascript</a></li>\n<li><a href=\"https://www.typescriptlang.org/docs/handbook/react-&#x26;-webpack.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">TypeScript Documentation: React and Webpack</a></li>\n</ul>\n<h2 id=\"reason\"><a href=\"#reason\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reason </h2>\n<p><a href=\"https://reasonml.github.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Reason</a> is not a new language; it’s a new syntax and toolchain powered by the battle-tested language, <a href=\"https://ocaml.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">OCaml</a>. Reason gives OCaml a familiar syntax geared toward JavaScript programmers, and caters to the existing NPM/Yarn workflow folks already know.</p>\n<p>Reason is developed at Facebook, and is used in some of its products like Messenger. It is still somewhat experimental but it has <a href=\"https://reasonml.github.io/reason-react/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">dedicated React bindings</a> maintained by Facebook and a <a href=\"https://reasonml.github.io/docs/en/community.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">vibrant community</a>.</p>\n<h2 id=\"kotlin\"><a href=\"#kotlin\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Kotlin </h2>\n<p><a href=\"https://kotlinlang.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Kotlin</a> is a statically typed language developed by JetBrains. Its target platforms include the JVM, Android, LLVM, and <a href=\"https://kotlinlang.org/docs/reference/js-overview.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">JavaScript</a>. </p>\n<p>JetBrains develops and maintains several tools specifically for the React community: <a href=\"https://github.com/JetBrains/kotlin-wrappers\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">React bindings</a> as well as <a href=\"https://github.com/JetBrains/create-react-kotlin-app\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Create React Kotlin App</a>. The latter helps you start building React apps with Kotlin with no build configuration.</p>\n<h2 id=\"other-languages\"><a href=\"#other-languages\" aria-hidden class=\"anchor\"><svg aria-hidden=\"true\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Other Languages </h2>\n<p>Note there are other statically typed languages that compile to JavaScript and are thus React compatible. For example, <a href=\"https://fable.io/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">F#/Fable</a> with <a href=\"https://elmish.github.io/react\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">elmish-react</a>. Check out their respective sites for more information, and feel free to add more statically typed languages that work with React to this page!</p>","frontmatter":{"title":"Static Type Checking","next":null,"prev":null},"fields":{"path":"content/docs/static-type-checking.md","slug":"docs/static-type-checking.html"}}},"pageContext":{"slug":"docs/static-type-checking.html"}},"staticQueryHashes":[]}