{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/react-&-webpack.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/mixins.html"},{"path":"/docs/handbook/module-resolution.html"},{"path":"/docs/handbook/modules.html"},{"path":"/docs/handbook/namespaces-and-modules.html"},{"path":"/docs/handbook/namespaces.html"},{"path":"/docs/handbook/nightly-builds.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/writing-declaration-files.html"},{"path":"/docs/handbook/declaration-files/by-example.html"},{"path":"/docs/handbook/declaration-files/consumption.html"},{"path":"/docs/handbook/declaration-files/deep-dive.html"},{"path":"/docs/handbook/declaration-files/do-s-and-don-ts.html"},{"path":"/docs/handbook/declaration-files/introduction.html"},{"path":"/docs/handbook/declaration-files/library-structures.html"},{"path":"/docs/handbook/declaration-files/publishing.html"},{"path":"/docs/handbook/declaration-files/templates.html"},{"path":"/docs/handbook/release-notes/typescript-1-1.html"},{"path":"/docs/handbook/release-notes/typescript-1-3.html"},{"path":"/docs/handbook/release-notes/typescript-1-4.html"},{"path":"/docs/handbook/release-notes/typescript-1-5.html"},{"path":"/docs/handbook/release-notes/typescript-1-6.html"},{"path":"/docs/handbook/release-notes/typescript-1-7.html"},{"path":"/docs/handbook/release-notes/typescript-1-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-0.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-3.html"},{"path":"/docs/handbook/release-notes/typescript-2-4.html"},{"path":"/docs/handbook/release-notes/typescript-2-5.html"},{"path":"/docs/handbook/release-notes/typescript-2-6.html"},{"path":"/docs/handbook/release-notes/typescript-2-7.html"},{"path":"/docs/handbook/release-notes/typescript-2-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-9.html"},{"path":"/docs/handbook/release-notes/typescript-3-0.html"},{"path":"/docs/handbook/release-notes/typescript-3-1.html"},{"path":"/docs/handbook/release-notes/typescript-3-2.html"},{"path":"/docs/handbook/release-notes/typescript-3-3.html"},{"path":"/docs/handbook/release-notes/typescript-3-4.html"},{"path":"/docs/handbook/release-notes/typescript-3-5.html"},{"path":"/docs/handbook/release-notes/typescript-3-6.html"},{"path":"/docs/handbook/release-notes/typescript-3-7.html"},{"path":"/docs/handbook/release-notes/typescript-3-8.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react-&-webpack.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-in-5-minutes-func.html"},{"path":"/docs/handbook/typescript-in-5-minutes.html"},{"path":"/docs/handbook/typescript-in-5-minutes-oop.html"},{"path":"/docs/handbook/typescript-from-scratch.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-class-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-function-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-d-ts.html"},{"path":"/docs/handbook/release-notes/overview.html"},{"path":"/tsconfig"},{"path":"/en/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/ja/play"},{"path":"/play/3-7/fixits/big-number-literals.ts"},{"path":"/play/3-7/fixits/const-to-let.ts"},{"path":"/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/play/3-7/types-and-code-flow/assertion-functions.ts"},{"path":"/play/3-7/types-and-code-flow/recursive-type-references.ts"},{"path":"/play/3-7/types-and-code-flow/uncalled-function-checks.ts"},{"path":"/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/play/3-8/jsdoc-improvements/accessibility-modifiers.js"},{"path":"/play/3-8/syntax-and-messaging/export-modules-from.ts"},{"path":"/play/3-8/syntax-and-messaging/private-class-fields.ts"},{"path":"/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/play/javascript/external-apis/typescript-with-node.js"},{"path":"/play/javascript/external-apis/typescript-with-web.js"},{"path":"/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/play/javascript/helping-with-javascript/errors.ts"},{"path":"/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/play/javascript/javascript-essentials/functions.ts"},{"path":"/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/play/javascript/modern-javascript/async-await.ts"},{"path":"/play/javascript/modern-javascript/immutability.ts"},{"path":"/play/javascript/modern-javascript/import-export.ts"},{"path":"/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/play/javascript/working-with-classes/classes-101.ts"},{"path":"/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/play/javascript/working-with-classes/mixins.ts"},{"path":"/play/javascript/working-with-classes/this.ts"},{"path":"/play/playground/config/javascript-playgrounds.js"},{"path":"/play/playground/config/new-compiler-defaults.ts"},{"path":"/play/playground/language/automatic-type-acquisition.ts"},{"path":"/play/playground/language/fixits.ts"},{"path":"/play/playground/tooling/mobile-support.ts"},{"path":"/play/playground/tooling/sharable-urls.ts"},{"path":"/play/playground/tooling/typescript-versions.ts"},{"path":"/play/typescript/language/soundness.ts"},{"path":"/play/typescript/language/structural-typing.ts"},{"path":"/play/typescript/language/type-guards.ts"},{"path":"/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/play/typescript/language-extensions/enums.ts"},{"path":"/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/play/typescript/meta-types/conditional-types.ts"},{"path":"/play/typescript/meta-types/discriminate-types.ts"},{"path":"/play/typescript/meta-types/indexed-types.ts"},{"path":"/play/typescript/meta-types/mapped-types.ts"},{"path":"/play/typescript/primitives/any.ts"},{"path":"/play/typescript/primitives/literals.ts"},{"path":"/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/play/typescript/primitives/unknown-and-never.ts"},{"path":"/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/play/typescript/type-primitives/nullable-types.ts"},{"path":"/play/typescript/type-primitives/tuples.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-node.js"},{"path":"/es/play/javascript/external-apis/typescript-with-web.js"},{"path":"/es/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/es/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/es/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/es/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/es/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/es/play/typescript/type-primitives/nullable-types.ts"},{"path":"/es/play/typescript/type-primitives/tuples.ts"},{"path":"/ja/play/typescript/language-extensions/enums.ts"},{"path":"/ja/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/ja/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/ja/play/typescript/primitives/any.ts"},{"path":"/ja/play/typescript/primitives/literals.ts"},{"path":"/ja/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/vo/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/vo/play/javascript/javascript-essentials/functions.ts"},{"path":"/vo/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/vo/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-node.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-web.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/zh/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/zh/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/zh/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/zh/play/javascript/helping-with-javascript/errors.ts"},{"path":"/zh/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/zh/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/zh/play/javascript/javascript-essentials/functions.ts"},{"path":"/zh/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/zh/play/javascript/modern-javascript/import-export.ts"},{"path":"/zh/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/zh/play/javascript/working-with-classes/classes-101.ts"},{"path":"/zh/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/zh/play/javascript/working-with-classes/mixins.ts"},{"path":"/zh/play/javascript/working-with-classes/this.ts"},{"path":"/zh/play/playground/config/javascript-playgrounds.js"},{"path":"/zh/play/playground/config/new-compiler-defaults.ts"},{"path":"/zh/play/playground/language/automatic-type-acquisition.ts"},{"path":"/zh/play/playground/language/fixits.ts"},{"path":"/zh/play/playground/tooling/mobile-support.ts"},{"path":"/zh/play/playground/tooling/sharable-urls.ts"},{"path":"/zh/play/playground/tooling/typescript-versions.ts"},{"path":"/zh/play/typescript/language/soundness.ts"},{"path":"/zh/play/typescript/language/structural-typing.ts"},{"path":"/zh/play/typescript/language/type-guards.ts"},{"path":"/zh/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/zh/play/typescript/language-extensions/enums.ts"},{"path":"/zh/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/zh/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/zh/play/typescript/meta-types/conditional-types.ts"},{"path":"/zh/play/typescript/meta-types/discriminate-types.ts"},{"path":"/zh/play/typescript/meta-types/indexed-types.ts"},{"path":"/zh/play/typescript/meta-types/mapped-types.ts"},{"path":"/zh/play/typescript/primitives/any.ts"},{"path":"/zh/play/typescript/primitives/literals.ts"},{"path":"/zh/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/zh/play/typescript/primitives/unknown-and-never.ts"},{"path":"/zh/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/zh/play/typescript/type-primitives/nullable-types.ts"},{"path":"/zh/play/typescript/type-primitives/tuples.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/community"},{"path":"/es/community"},{"path":"/ja/community"},{"path":"/vo/community"},{"path":"/zh/community"},{"path":"/download"},{"path":"/es/download"},{"path":"/ja/download"},{"path":"/vo/download"},{"path":"/zh/download"},{"path":"/empty"},{"path":"/es/empty"},{"path":"/ja/empty"},{"path":"/vo/empty"},{"path":"/zh/empty"},{"path":"/"},{"path":"/es/"},{"path":"/ja/"},{"path":"/vo/"},{"path":"/zh/"},{"path":"/tools"},{"path":"/es/tools"},{"path":"/ja/tools"},{"path":"/vo/tools"},{"path":"/zh/tools"},{"path":"/why-create-typescript"},{"path":"/es/why-create-typescript"},{"path":"/ja/why-create-typescript"},{"path":"/vo/why-create-typescript"},{"path":"/zh/why-create-typescript"},{"path":"/docs/home"},{"path":"/es/docs/home"},{"path":"/ja/docs/home"},{"path":"/vo/docs/home"},{"path":"/zh/docs/home"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"}]},"markdownRemark":{"id":"2dbc00b2-df4a-589c-a9eb-4a7ed9884c57","excerpt":"This guide will teach you how to wire up TypeScript with React and webpack. If you’re starting a brand new project, take a look at the React Quick Start guide…","html":"<p>This guide will teach you how to wire up TypeScript with <a href=\"https://reactjs.org/\">React</a> and <a href=\"https://webpack.js.org/\">webpack</a>.</p>\n<p>If you’re starting a brand new project, take a look at the <a href=\"/samples/index.html\">React Quick Start guide</a> first.</p>\n<p>Otherwise, we assume that you’re already using <a href=\"https://nodejs.org/\">Node.js</a> with <a href=\"https://www.npmjs.com/\">npm</a>.</p>\n<h1 id=\"lay-out-the-project\" style=\"position:relative;\"><a href=\"#lay-out-the-project\" aria-label=\"lay out the project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" 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>Lay out the project</h1>\n<p>Let’s start out with a new directory.\nWe’ll name it <code>proj</code> for now, but you can change it to whatever you want.</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">mkdir proj</span>\n<span style=\"color: #000000\">cd proj</span></code></div></pre>\n<p>To start, we’re going to structure our project in the following way:</p>\n<pre><code>proj/\n├─ dist/\n└─ src/\n   └─ components/\n</code></pre>\n<p>TypeScript files will start out in your <code>src</code> folder, run through the TypeScript compiler, then webpack, and end up in a <code>main.js</code> file in <code>dist</code>.\nAny components that we write will go in the <code>src/components</code> folder.</p>\n<p>Let’s scaffold this out:</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">mkdir src</span>\n<span style=\"color: #000000\">cd src</span>\n<span style=\"color: #000000\">mkdir components</span>\n<span style=\"color: #000000\">cd ..</span></code></div></pre>\n<p>Webpack will eventually generate the <code>dist</code> directory for us.</p>\n<h1 id=\"initialize-the-project\" style=\"position:relative;\"><a href=\"#initialize-the-project\" aria-label=\"initialize the project permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" 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>Initialize the project</h1>\n<p>Now we’ll turn this folder into an npm package.</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npm init -y</span></code></div></pre>\n<p>This creates a <code>package.json</code> file with default values.</p>\n<h1 id=\"install-our-dependencies\" style=\"position:relative;\"><a href=\"#install-our-dependencies\" aria-label=\"install our dependencies permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" 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>Install our dependencies</h1>\n<p>First ensure Webpack is installed.</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npm install --save-dev webpack webpack-cli</span></code></div></pre>\n<p>Webpack is a tool that will bundle your code and optionally all of its dependencies into a single <code>.js</code> file.</p>\n<p>Let’s now add React and React-DOM, along with their declaration files, as dependencies to your <code>package.json</code> file:</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npm install --save react react-dom</span>\n<span style=\"color: #000000\">npm install --save-dev @types/react @types/react-dom</span></code></div></pre>\n<p>That <code>@types/</code> prefix means that we also want to get the declaration files for React and React-DOM.\nUsually when you import a path like <code>\"react\"</code>, it will look inside of the <code>react</code> package itself;\nhowever, not all packages include declaration files, so TypeScript also looks in the <code>@types/react</code> package as well.\nYou’ll see that we won’t even have to think about this later on.</p>\n<p>Next, we’ll add development-time dependencies on the <a href=\"https://www.npmjs.com/package/ts-loader\">ts-loader</a> and <a href=\"https://www.npmjs.com/package/source-map-loader\">source-map-loader</a>.</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npm install --save-dev typescript ts-loader source-map-loader</span></code></div></pre>\n<p>Both of these dependencies will let TypeScript and webpack play well together.\nts-loader helps Webpack compile your TypeScript code using the TypeScript’s standard configuration file named <code>tsconfig.json</code>.\nsource-map-loader uses any sourcemap outputs from TypeScript to inform webpack when generating <em>its own</em> sourcemaps.\nThis will allow you to debug your final output file as if you were debugging your original TypeScript source code.</p>\n<p>Please note that ts-loader is not the only loader for typescript.\nYou could instead use <a href=\"https://www.npmjs.com/package/awesome-typescript-loader\">awesome-typescript-loader</a>.</p>\n<p>Read about the differences between them <a href=\"https://github.com/s-panferov/awesome-typescript-loader#differences-between-ts-loader\">here</a>.</p>\n<p>Notice that we installed TypeScript as a development dependency.\nWe could also have linked TypeScript to a global copy with <code>npm link typescript</code>, but this is a less common scenario.</p>\n<h1 id=\"add-a-typescript-configuration-file\" style=\"position:relative;\"><a href=\"#add-a-typescript-configuration-file\" aria-label=\"add a typescript configuration file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" 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>Add a TypeScript configuration file</h1>\n<p>You’ll want to bring your TypeScript files together - both the code you’ll be writing as well as any necessary declaration files.</p>\n<p>To do this, you’ll need to create a <code>tsconfig.json</code> which contains a list of your input files as well as all your compilation settings.\nSimply create a new file in your project root named <code>tsconfig.json</code> and fill it with the following contents:</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"outDir\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"./dist/\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"sourceMap\"</span><span style=\"color: #000000\">: </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"noImplicitAny\"</span><span style=\"color: #000000\">: </span><span style=\"color: #0000FF\">true</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"module\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"commonjs\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"target\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"es6\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0451A5\">\"jsx\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"react\"</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>You can learn more about <code>tsconfig.json</code> files <a href=\"/fa4665f537f8fffb14388d397744045a/tsconfig.json.md\">here</a>.</p>\n<h1 id=\"write-some-code\" style=\"position:relative;\"><a href=\"#write-some-code\" aria-label=\"write some code permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" 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>Write some code</h1>\n<p>Let’s write our first TypeScript file using React.\nFirst, create a file named <code>Hello.tsx</code> in <code>src/components</code> and write the following:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> React </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"react\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> HelloProps { compiler: string; framework: string; }</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">const</span><span style=\"color: #000000\"> Hello = (props: HelloProps) </span><span style=\"color: #0000FF\">=&gt;</span><span style=\"color: #000000\"> &lt;h1&gt;Hello from {props.compiler} and {props.framework}!&lt;/h1&gt;;</span></code></div></pre>\n<p>Note that while this example uses <a href=\"https://reactjs.org/docs/components-and-props.html#functional-and-class-components\">function components</a>, we could also make our example a little <em>classier</em> as well.</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> React </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"react\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">interface</span><span style=\"color: #000000\"> HelloProps { compiler: string; framework: string; }</span>\n\n<span style=\"color: #008000\">// 'HelloProps' describes the shape of props.</span>\n<span style=\"color: #008000\">// State is never set so we use the '{}' type.</span>\n<span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">class</span><span style=\"color: #000000\"> Hello </span><span style=\"color: #0000FF\">extends</span><span style=\"color: #000000\"> React.Component&lt;HelloProps, {}&gt; {</span>\n<span style=\"color: #000000\">    render() {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #0000FF\">return</span><span style=\"color: #000000\"> &lt;h1&gt;Hello from {</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.props.compiler} and {</span><span style=\"color: #0000FF\">this</span><span style=\"color: #000000\">.props.framework}!&lt;/h1&gt;;</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Next, let’s create an <code>index.tsx</code> in <code>src</code> with the following source:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> React </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"react\"</span><span style=\"color: #000000\">;</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> ReactDOM </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"react-dom\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> { Hello } </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"./components/Hello\"</span><span style=\"color: #000000\">;</span>\n\n<span style=\"color: #000000\">ReactDOM.render(</span>\n<span style=\"color: #000000\">    &lt;Hello compiler=</span><span style=\"color: #A31515\">\"TypeScript\"</span><span style=\"color: #000000\"> framework=</span><span style=\"color: #A31515\">\"React\"</span><span style=\"color: #000000\"> /&gt;,</span>\n<span style=\"color: #000000\">    document.getElementById(</span><span style=\"color: #A31515\">\"example\"</span><span style=\"color: #000000\">)</span>\n<span style=\"color: #000000\">);</span></code></div></pre>\n<p>We just imported our <code>Hello</code> component into <code>index.tsx</code>.\nNotice that unlike with <code>\"react\"</code> or <code>\"react-dom\"</code>, we used a <em>relative path</em> to <code>Hello.tsx</code> - this is important.\nIf we hadn’t, TypeScript would’ve instead tried looking in our <code>node_modules</code> folder.</p>\n<p>We’ll also need a page to display our <code>Hello</code> component.\nCreate a file at the root of <code>proj</code> named <code>index.html</code> with the following contents:</p>\n<pre class=\"shiki\"><div class=\"language-id\">html</div><div class='code-container'><code><span style=\"color: #800000\">&lt;!</span><span style=\"color: #000000\">DOCTYPE html</span><span style=\"color: #800000\">&gt;</span>\n<span style=\"color: #800000\">&lt;html&gt;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #800000\">&lt;head&gt;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #800000\">&lt;meta</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">charset</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"UTF-8\"</span><span style=\"color: #800000\"> /&gt;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #800000\">&lt;title&gt;</span><span style=\"color: #000000\">Hello React!</span><span style=\"color: #800000\">&lt;/title&gt;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #800000\">&lt;/head&gt;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #800000\">&lt;body&gt;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #800000\">&lt;div</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">id</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"example\"</span><span style=\"color: #800000\">&gt;&lt;/div&gt;</span>\n\n<span style=\"color: #000000\">        </span><span style=\"color: #008000\">&lt;!-- Dependencies --&gt;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #800000\">&lt;script</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">src</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"./node_modules/react/umd/react.development.js\"</span><span style=\"color: #800000\">&gt;&lt;/script&gt;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #800000\">&lt;script</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">src</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"./node_modules/react-dom/umd/react-dom.development.js\"</span><span style=\"color: #800000\">&gt;&lt;/script&gt;</span>\n\n<span style=\"color: #000000\">        </span><span style=\"color: #008000\">&lt;!-- Main --&gt;</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #800000\">&lt;script</span><span style=\"color: #000000\"> </span><span style=\"color: #DC0000\">src</span><span style=\"color: #000000\">=</span><span style=\"color: #0000FF\">\"./dist/main.js\"</span><span style=\"color: #800000\">&gt;&lt;/script&gt;</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #800000\">&lt;/body&gt;</span>\n<span style=\"color: #800000\">&lt;/html&gt;</span></code></div></pre>\n<p>Notice that we’re including files from within <code>node_modules</code>.\nReact and React-DOM’s npm packages include standalone <code>.js</code> files that you can include in a web page, and we’re referencing them directly to get things moving faster.\nFeel free to copy these files to another directory, or alternatively, host them on a content delivery network (CDN).\nFacebook makes CDN-hosted versions of React available, and you can <a href=\"http://facebook.github.io/react/downloads.html#development-vs.-production-builds\">read more about that here</a>.</p>\n<h1 id=\"create-a-webpack-configuration-file\" style=\"position:relative;\"><a href=\"#create-a-webpack-configuration-file\" aria-label=\"create a webpack configuration file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" 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 a webpack configuration file</h1>\n<p>Create a <code>webpack.config.js</code> file at the root of the project directory.</p>\n<pre class=\"shiki\"><div class=\"language-id\">js</div><div class='code-container'><code><span style=\"color: #000000\">module.exports = {</span>\n<span style=\"color: #000000\">    mode: </span><span style=\"color: #A31515\">\"production\"</span><span style=\"color: #000000\">,</span>\n\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// Enable sourcemaps for debugging webpack's output.</span>\n<span style=\"color: #000000\">    devtool: </span><span style=\"color: #A31515\">\"source-map\"</span><span style=\"color: #000000\">,</span>\n\n<span style=\"color: #000000\">    resolve: {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #008000\">// Add '.ts' and '.tsx' as resolvable extensions.</span>\n<span style=\"color: #000000\">        extensions: [</span><span style=\"color: #A31515\">\".ts\"</span><span style=\"color: #000000\">, </span><span style=\"color: #A31515\">\".tsx\"</span><span style=\"color: #000000\">]</span>\n<span style=\"color: #000000\">    },</span>\n\n<span style=\"color: #000000\">    module: {</span>\n<span style=\"color: #000000\">        rules: [</span>\n<span style=\"color: #000000\">            {</span>\n<span style=\"color: #000000\">                test:</span><span style=\"color: #A31515\"> /\\.ts(x</span><span style=\"color: #000000\">?</span><span style=\"color: #A31515\">)</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">                exclude:</span><span style=\"color: #A31515\"> /node_modules/</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">                use: [</span>\n<span style=\"color: #000000\">                    {</span>\n<span style=\"color: #000000\">                        loader: </span><span style=\"color: #A31515\">\"ts-loader\"</span>\n<span style=\"color: #000000\">                    }</span>\n<span style=\"color: #000000\">                ]</span>\n<span style=\"color: #000000\">            },</span>\n<span style=\"color: #000000\">            </span><span style=\"color: #008000\">// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.</span>\n<span style=\"color: #000000\">            {</span>\n<span style=\"color: #000000\">                enforce: </span><span style=\"color: #A31515\">\"pre\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">                test:</span><span style=\"color: #A31515\"> /\\.js</span><span style=\"color: #0000FF\">$</span><span style=\"color: #A31515\">/</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">                loader: </span><span style=\"color: #A31515\">\"source-map-loader\"</span>\n<span style=\"color: #000000\">            }</span>\n<span style=\"color: #000000\">        ]</span>\n<span style=\"color: #000000\">    },</span>\n\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// When importing a module whose path matches one of the following, just</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// assume a corresponding global variable exists and use that instead.</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// This is important because it allows us to avoid bundling all of our</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #008000\">// dependencies, which allows browsers to cache those libraries between builds.</span>\n<span style=\"color: #000000\">    externals: {</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #A31515\">\"react\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"React\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #A31515\">\"react-dom\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\"ReactDOM\"</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">};</span></code></div></pre>\n<p>You might be wondering about that <code>externals</code> field.\nWe want to avoid bundling all of React into the same file, since this increases compilation time and browsers will typically be able to cache a library if it doesn’t change.</p>\n<p>Ideally, we’d just import the React module from within the browser, but most browsers still don’t quite support modules yet.\nInstead libraries have traditionally made themselves available using a single global variable like <code>jQuery</code> or <code>_</code>.\nThis is called the “namespace pattern”, and webpack allows us to continue leveraging libraries written that way.\nWith our entry for <code>\"react\": \"React\"</code>, webpack will work its magic to make any import of <code>\"react\"</code> load from the <code>React</code> variable.</p>\n<p>You can learn more about configuring webpack <a href=\"https://webpack.js.org/concepts\">here</a>.</p>\n<h1 id=\"putting-it-all-together\" style=\"position:relative;\"><a href=\"#putting-it-all-together\" aria-label=\"putting it all together permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" 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>Putting it all together</h1>\n<p>Just run:</p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">npx webpack</span></code></div></pre>\n<p>Now open up <code>index.html</code> in your favorite browser and everything should be ready to use!\nYou should see a page that says “Hello from TypeScript and React!”</p>","headings":[{"value":"Lay out the project","depth":1},{"value":"Initialize the project","depth":1},{"value":"Install our dependencies","depth":1},{"value":"Add a TypeScript configuration file","depth":1},{"value":"Write some code","depth":1},{"value":"Create a webpack configuration file","depth":1},{"value":"Putting it all together","depth":1}],"frontmatter":{"permalink":"/docs/handbook/react-&-webpack.html","title":"React & Webpack"}}},"pageContext":{"slug":"/docs/handbook/react-&-webpack.html","isOldHandbook":true}}}