<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Tom Duncalf’s Blog</title>
    <link>http://blog.tomduncalf.com/</link>
    <description>Recent content on Tom Duncalf’s Blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <lastBuildDate>Fri, 24 Jun 2016 10:22:10 +0100</lastBuildDate>
    <atom:link href="http://blog.tomduncalf.com/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Functional Programming Fundamentals Talk</title>
      <link>http://blog.tomduncalf.com/posts/functional-programming-fundamentals-talk/</link>
      <pubDate>Fri, 24 Jun 2016 10:22:10 +0100</pubDate>
      
      <guid>http://blog.tomduncalf.com/posts/functional-programming-fundamentals-talk/</guid>
      <description>&lt;p&gt;In February 2016, I gave a talk at &lt;a href=&#34;https://red-badger.com/&#34;&gt;Red Badger&lt;/a&gt;&amp;rsquo;s &lt;a href=&#34;http://www.meetup.com/Functional-Programming-for-the-Web/&#34;&gt;Functional Programming for the Web&lt;/a&gt; meetup in London, on the topic of &amp;ldquo;Functional Programming Fundamentals&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;The talk explains topics such as first class functions, composition, currying, functors and monads in a (hopefully!) simple way for newcomers to FP, illustrated using JavaScript examples which are available on &lt;a href=&#34;http://bit.ly/FPFundamentalsCode&#34;&gt;GitHub&lt;/a&gt;, as are the &lt;a href=&#34;http://bit.ly/FPFundementals&#34;&gt;slides&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The talk was inspired by &lt;a href=&#34;https://twitter.com/drboolean&#34;&gt;Dr. Boolean&lt;/a&gt;&amp;rsquo;s excellent free e-book, &lt;a href=&#34;https://github.com/MostlyAdequate/mostly-adequate-guide&#34;&gt;Professor Frisby&amp;rsquo;s Mostly Adequate Guide to Functional Programming&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can watch the talk on &lt;a href=&#34;http://bit.ly/FPFundementalsTalk&#34;&gt;YouTube&lt;/a&gt; or below:&lt;/p&gt;


&lt;div style=&#34;position: relative; padding-bottom: 56.25%; padding-top: 30px; height: 0; overflow: hidden;&#34;&gt;
  &lt;iframe src=&#34;//www.youtube.com/embed/xyqTHLOra_I&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%;&#34; allowfullscreen frameborder=&#34;0&#34;&gt;&lt;/iframe&gt;
 &lt;/div&gt;

</description>
    </item>
    
    <item>
      <title>Unit Testing React Applications with Mocha, Chai and Enzyme Talk</title>
      <link>http://blog.tomduncalf.com/posts/react-unit-testing-with-mocha-chai-enzyme-talk/</link>
      <pubDate>Fri, 24 Jun 2016 10:22:10 +0100</pubDate>
      
      <guid>http://blog.tomduncalf.com/posts/react-unit-testing-with-mocha-chai-enzyme-talk/</guid>
      <description>&lt;p&gt;In April 2016, I gave a talk at &lt;a href=&#34;https://red-badger.com/&#34;&gt;Red Badger&lt;/a&gt;&amp;rsquo;s &lt;a href=&#34;http://www.meetup.com/London-React-User-Group/&#34;&gt;React London User Group&lt;/a&gt; meetup in London, on the topic of &amp;ldquo;Unit Testing React Applications with Mocha, Chai and Enzyme&amp;rdquo;.&lt;/p&gt;

&lt;p&gt;The talk introduces the fundamentals of testing a React application with AirBnB&amp;rsquo;s excellent &lt;a href=&#34;https://github.com/airbnb/enzyme&#34;&gt;Enzyme&lt;/a&gt; library, and explains my approach to both unit and integration testing apps using the combination of &lt;a href=&#34;https://mochajs.org/&#34;&gt;Mocha&lt;/a&gt;, &lt;a href=&#34;http://chaijs.com/&#34;&gt;Chai&lt;/a&gt; and &lt;a href=&#34;https://github.com/airbnb/enzyme&#34;&gt;Enzyme&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Both the &lt;a href=&#34;http://bit.ly/ReactTestingCode&#34;&gt;code samples&lt;/a&gt;, and the &lt;a href=&#34;http://bit.ly/ReactTestingPresentation&#34;&gt;slides&lt;/a&gt; are available on GitHub.&lt;/p&gt;

&lt;p&gt;You can watch the talk on &lt;a href=&#34;http://bit.ly/ReactTestingTalk&#34;&gt;YouTube&lt;/a&gt; or below:&lt;/p&gt;


&lt;div style=&#34;position: relative; padding-bottom: 56.25%; padding-top: 30px; height: 0; overflow: hidden;&#34;&gt;
  &lt;iframe src=&#34;//www.youtube.com/embed/kkqArkYV4fw&#34; style=&#34;position: absolute; top: 0; left: 0; width: 100%; height: 100%;&#34; allowfullscreen frameborder=&#34;0&#34;&gt;&lt;/iframe&gt;
 &lt;/div&gt;

</description>
    </item>
    
    <item>
      <title>Setting up a new Typescript 1.9 and React project</title>
      <link>http://blog.tomduncalf.com/posts/setting-up-typescript-and-react/</link>
      <pubDate>Tue, 21 Jun 2016 19:46:14 +0100</pubDate>
      
      <guid>http://blog.tomduncalf.com/posts/setting-up-typescript-and-react/</guid>
      <description>

&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;

&lt;p&gt;This post is a brain dump of the steps required to set up a Typescript and React project with some explanatory notes – I intend to write about working with Typescript and React in a real world project in more detail soon. Some knowledge of React and of the basics of Typescript is assumed.&lt;/p&gt;

&lt;p&gt;These instructions will guide you through setting up a new project with Typescript, React, Webpack and Babel – neither Webpack nor Babel are required to work with Typescript, as Typescript can transpile ES6 to ES5 and do some degree of bundling itself; but using them enables Hot Module Reloading, and also allows you to run other Babel and Webpack plugins on the compiled output if desired.&lt;/p&gt;

&lt;p&gt;The resulting project template is available on Github at &lt;a href=&#34;https://github.com/tomduncalf/typescript-react-template&#34;&gt;https://github.com/tomduncalf/typescript-react-template&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&#34;editor-setup&#34;&gt;Editor setup&lt;/h1&gt;

&lt;h2 id=&#34;atom&#34;&gt;Atom&lt;/h2&gt;

&lt;p&gt;Atom users will need to install the &lt;code&gt;atom-typescript&lt;/code&gt; plugin, which is excellent, but currently doesn&amp;rsquo;t support some of the features in Typescript 1.9, and seems to be &lt;a href=&#34;https://github.com/TypeStrong/atom-typescript/pull/849&#34;&gt;lacking an active project leader&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&#34;sublime&#34;&gt;Sublime&lt;/h2&gt;

&lt;p&gt;Sublime users will need the official Microsoft &lt;code&gt;Typescript&lt;/code&gt; package (&lt;a href=&#34;https://github.com/Microsoft/TypeScript-Sublime-Plugin&#34;&gt;https://github.com/Microsoft/TypeScript-Sublime-Plugin&lt;/a&gt;), which is also very good.&lt;/p&gt;

&lt;h2 id=&#34;visual-studio-code&#34;&gt;Visual Studio Code&lt;/h2&gt;

&lt;p&gt;Visual Studio Code users don&amp;rsquo;t need to install anything – it&amp;rsquo;s already set up for Typescript.&lt;/p&gt;

&lt;h2 id=&#34;recommendation&#34;&gt;Recommendation&lt;/h2&gt;

&lt;p&gt;All of these editor/plugin combinations work well, and will automatically detect Typescript projects and offer the appropriate autocompletion and error highlighting.&lt;/p&gt;

&lt;p&gt;Visual Studio Code has the best Typescript integration (as you might expect from Microsoft!) and is noticeably faster than Atom. In the past, I was put off using VS Code due to the lack of tabs, but as these are now available in the latest &lt;a href=&#34;http://code.visualstudio.com/Download#insiders&#34;&gt;Insiders build&lt;/a&gt; by &lt;a href=&#34;https://github.com/Microsoft/vscode-docs/blob/vnext/release-notes/June_2016.md#tabs&#34;&gt;enabling a flag&lt;/a&gt;, it&amp;rsquo;s now my recommended editor.&lt;/p&gt;

&lt;p&gt;The Atom and Sublime plugins offer broadly similar functionality, and I have always used Atom in the past, but I can&amp;rsquo;t fully recommend it at the minute due to the uncertain status of the &lt;code&gt;atom-typescript&lt;/code&gt; plugin and its lack of support for Typescript 1.9 features (although this is fair enough, as it is still a beta). The Sublime plugin is an official Microsoft product, so shouldn&amp;rsquo;t suffer from the same issues, and is a great choice if you are already a Sublime user.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; If you are using Visual Studio Code, see step 1 for additional setup required to make it use the &lt;code&gt;typescript@next&lt;/code&gt; compiler.&lt;/p&gt;

&lt;h2 id=&#34;linting&#34;&gt;Linting&lt;/h2&gt;

&lt;p&gt;If you want to lint your code, you&amp;rsquo;ll want to install a &lt;code&gt;tslint&lt;/code&gt; plugin for your editor and setup &lt;code&gt;tslint&lt;/code&gt; – see the project page at &lt;a href=&#34;https://github.com/palantir/tslint/&#34;&gt;https://github.com/palantir/tslint/&lt;/a&gt; for more information.&lt;/p&gt;

&lt;h1 id=&#34;basic-project-setup&#34;&gt;Basic project setup&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install Typescript globally&lt;/strong&gt; - this is optional, but it&amp;rsquo;s handy to have the Typescript compiler &lt;code&gt;tsc&lt;/code&gt; available globally at the command line. We will use &lt;code&gt;typescript@next&lt;/code&gt; (version 1.9.x) even though it is still in beta, as it supports installing type declarations from &lt;code&gt;npm&lt;/code&gt; rather than having to use the &lt;code&gt;typings&lt;/code&gt; tool, which is &lt;a href=&#34;https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/&#34;&gt;the future of working with type declaration files&lt;/a&gt; and makes life much easier.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm install -g typescript@next
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;note-for-vs-code-users&#34;&gt;Note for VS Code users&lt;/h2&gt;

&lt;p&gt;If you are using VS Code, you need to tell it to use the Typescript compiler that you installed with &lt;code&gt;npm&lt;/code&gt;, otherwise you&amp;rsquo;ll get syntax errors as the internal VS Code Typescript compiler is v1.8 and so doesn&amp;rsquo;t support type declarations installed from &lt;code&gt;npm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To do so, you&amp;rsquo;ll need to know where &lt;code&gt;npm&lt;/code&gt; has installed your global packages to – you can check this with:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm list -g | head -n1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then open up your user settings in VS Code (&lt;code&gt;Cmd&lt;/code&gt; + &lt;code&gt;,&lt;/code&gt; on a Mac) and add a line to your user settings, pointing the &lt;code&gt;typescript.tsdk&lt;/code&gt; option to the &lt;code&gt;node_modules/typescript/lib&lt;/code&gt; directory in this location – for example, if the global packages are installed to &lt;code&gt;/Users/td/.nvm/versions/node/v5.5.0/lib/&lt;/code&gt;, then add the following and restart Code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{
  &amp;quot;typescript.tsdk&amp;quot;: &amp;quot;/Users/td/.nvm/versions/node/v5.5.0/lib/node_modules/typescript/lib&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a new project&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;mkdir my-project &amp;amp;&amp;amp; cd my-project
git init
npm init -y
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialise the project for Typescript&lt;/strong&gt; - install Typescript as a dev dependency and create a skeleton &lt;code&gt;tsconfig.json&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm i -D typescript@next
tsc --init
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&#34;setting-up-tsconfig-json&#34;&gt;Setting up tsconfig.json&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tsconfig.json&lt;/code&gt; controls the behaviour of the Typescript compiler (&lt;code&gt;tsc&lt;/code&gt;). There are a few settings which are useful for React and Babel projects which differ from the defaults. &lt;strong&gt;Open &lt;code&gt;tsconfig.json&lt;/code&gt; and change it to the following&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{
  &amp;quot;compilerOptions&amp;quot;: {
    &amp;quot;module&amp;quot;: &amp;quot;es6&amp;quot;,
    &amp;quot;target&amp;quot;: &amp;quot;es6&amp;quot;,
    &amp;quot;moduleResolution&amp;quot;: &amp;quot;node&amp;quot;,
    &amp;quot;baseUrl&amp;quot;: &amp;quot;src&amp;quot;,
    &amp;quot;allowSyntheticDefaultImports&amp;quot;: true,
    &amp;quot;noImplicitAny&amp;quot;: false,
    &amp;quot;sourceMap&amp;quot;: true,
    &amp;quot;outDir&amp;quot;: &amp;quot;ts-build&amp;quot;,
    &amp;quot;jsx&amp;quot;: &amp;quot;preserve&amp;quot;
  },
  &amp;quot;exclude&amp;quot;: [
    &amp;quot;node_modules&amp;quot;
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;An explanation of what this all means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;module&amp;quot;: &amp;quot;es6&amp;quot;&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; to output code which uses the ES6 module spec (i.e. &lt;code&gt;import&lt;/code&gt; statements). It&amp;rsquo;s also possible to set this to e.g. &lt;code&gt;commonjs&lt;/code&gt;, in which case &lt;code&gt;tsc&lt;/code&gt; will convert your code to that module spec, but as we will be putting our compiled JS code through Webpack, we can keep it as ES6 and let Webpack handle the module bundling.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;target&amp;quot;: &amp;quot;es6&amp;quot;&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; to output ES6 rather than ES5 Javascript code. We want this as we will be running the compiled JS through Babel, and keeping ES6 code intact after compilation can be useful for some Babel plugins (e.g. &lt;a href=&#34;https://www.npmjs.com/package/babel-plugin-transform-react-stateless-component-name&#34;&gt;&lt;code&gt;transform-react-stateless-component-name&lt;/code&gt;&lt;/a&gt;, which automatically names stateless components, will only pick up on arrow functions).&lt;/p&gt;

&lt;p&gt;This also allows us to use &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;, which is understood by Typescript but not currently able to be transpiled – instead, we can use Babel to handle the transpilation of &lt;code&gt;async&lt;/code&gt; code. If we weren&amp;rsquo;t using Babel, this setting could be omitted or set to &lt;code&gt;es5&lt;/code&gt;, to make &lt;code&gt;tsc&lt;/code&gt; do the transpilation of ES6 to ES5 itself.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;moduleResolution&amp;quot;: &amp;quot;node&amp;quot;&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; to use the Node module resolution strategy. This allows Typescript to load type declarations supplied alongside &lt;code&gt;npm&lt;/code&gt; packages (e.g. MobX includes its own type declarations in the main &lt;code&gt;mobx&lt;/code&gt; package), and with the &lt;code&gt;baseUrl&lt;/code&gt; option, allows us to use absolute-style imports for local modules.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;baseUrl&amp;quot;: &amp;quot;src&amp;quot;&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; to look in &lt;code&gt;src&lt;/code&gt; for any modules we import that aren&amp;rsquo;t found in &lt;code&gt;node_modules&lt;/code&gt;. This allows us to write absolute-style imports for local modules, e.g. &lt;code&gt;import Whatever from &#39;components/Whatever&#39;&lt;/code&gt; rather than &lt;code&gt;import Whatever from &#39;../components/Whatever&#39;&lt;/code&gt;, which is great for one&amp;rsquo;s sanity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note that this settings does not work with the current version of &lt;code&gt;atom-typescript&lt;/code&gt;&lt;/strong&gt; (see &lt;a href=&#34;https://github.com/TypeStrong/atom-typescript/pull/849&#34;&gt;https://github.com/TypeStrong/atom-typescript/pull/849&lt;/a&gt;) – remove this line from your &lt;code&gt;tsconfig.json&lt;/code&gt; if you want to use Atom. You can get an approximation of the absolute-style import by using &lt;code&gt;&amp;quot;moduleResolution&amp;quot;: &amp;quot;classic&amp;quot;&lt;/code&gt; (which will walk up the directory tree until a match is found, so not the same behaviour, but similar end result in many cases), but this breaks the ability to automatically import type declarations supplied with &lt;code&gt;npm&lt;/code&gt; packages.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;allowSyntheticDefaultImports&amp;quot;: true&lt;/code&gt; allows us to use ES6 &lt;code&gt;import&lt;/code&gt; syntax for &lt;code&gt;npm&lt;/code&gt; modules which don&amp;rsquo;t have a default export.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;noImplicitAny&amp;quot;: false&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; not to warn us if any variables are inferred as having a type of &lt;code&gt;any&lt;/code&gt;. It&amp;rsquo;s actually probably good practice to set this to &lt;code&gt;true&lt;/code&gt;, but it does mean you&amp;rsquo;ll potentially have to be more liberal with type annotations.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;sourceMap&amp;quot;: true&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; to output a source map, which enables easier debugging from the browser as it can tell you where in the original &lt;code&gt;.ts&lt;/code&gt; source file an error occurred, rather than just in the compiled &lt;code&gt;.js&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;outDir&amp;quot;: &amp;quot;ts-build&amp;quot;&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; to output the compiled &lt;code&gt;.js&lt;/code&gt; files to a directory called &lt;code&gt;ts-build&lt;/code&gt; (which can be &lt;code&gt;.gitignore&lt;/code&gt;d). The default is to output them alongside the original &lt;code&gt;.ts&lt;/code&gt; source files, but this gets messy. It should be noted that most of the time, we won&amp;rsquo;t be outputting the compiled &lt;code&gt;.js&lt;/code&gt; to disk, as the Webpack loader will do the compilation in memory, but it is sometimes useful to be able to invoke &lt;code&gt;tsc&lt;/code&gt; manually and inspect the compiled output.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;&amp;quot;jsx&amp;quot;: &amp;quot;preserve&amp;quot;&lt;/code&gt; tells &lt;code&gt;tsc&lt;/code&gt; to leave JSX code as it is, meaning that something else (in this case, Babel) is responsible for compiling it down to &lt;code&gt;React.createElement&lt;/code&gt; function calls. It is possible to set this to &lt;code&gt;&amp;quot;react&amp;quot;&lt;/code&gt; instead, which will cause &lt;code&gt;tsc&lt;/code&gt; to output &lt;code&gt;React.createElement&lt;/code&gt; calls directly, but it can be useful to have the raw JSX available to Babel, e.g. for plugins to process.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;We &lt;code&gt;exclude&lt;/code&gt; &lt;code&gt;node_modules&lt;/code&gt; as we don&amp;rsquo;t want &lt;code&gt;tsc&lt;/code&gt; to try and compile anything it finds in there – it&amp;rsquo;s alternatively possible to explicitly &lt;code&gt;include&lt;/code&gt; files for compilation (or use the non-standard &lt;code&gt;filesGlob&lt;/code&gt; option, which allows you to specify wildcard patterns, supported by the Atom plugin and &lt;a href=&#34;https://github.com/TypeStrong/tsconfig&#34;&gt;https://github.com/TypeStrong/tsconfig&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&#34;adding-react-to-the-project&#34;&gt;Adding React to the project&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install React to &lt;code&gt;node_modules&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm i -S react
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;To demonstrate what we are about to do, &lt;strong&gt;&lt;code&gt;mkdir src&lt;/code&gt; and create a file &lt;code&gt;index.tsx&lt;/code&gt; in there&lt;/strong&gt; containing a basic (stateless) React component:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-javascript&#34;&gt;import * as React from &#39;react&#39;;

export default () =&amp;gt; &amp;lt;div&amp;gt;Hello world&amp;lt;/div&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If your editor is set up correctly, you should already see that it has highlighted an issue with the &lt;code&gt;import * as React from &#39;react&#39;;&lt;/code&gt; line, but to demonstrate further, run &lt;code&gt;tsc&lt;/code&gt; from your project root and you should get the following output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;src/index.tsx(1,24): error TS2307: Cannot find module &#39;react&#39;.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The issue here is that the Typescript type declarations for React aren&amp;rsquo;t installed, and so Typescript says it can&amp;rsquo;t find the module, as type declarations are required for any modules that are &lt;code&gt;import&lt;/code&gt;ed (the error message doesn&amp;rsquo;t make this especially clear!).&lt;/p&gt;

&lt;p&gt;Something interesting to note, however, is that &lt;code&gt;tsc&lt;/code&gt; has created a &lt;code&gt;ts-build&lt;/code&gt; directory and written &lt;code&gt;index.jsx&lt;/code&gt; there, with sensible contents – in general, the Typescript compiler will try and emit code even if there are errors, as long as they aren&amp;rsquo;t fatal (although this can be disabled in &lt;code&gt;tsconfig.json&lt;/code&gt; with the &lt;code&gt;noEmitOnError&lt;/code&gt; option).&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Before installing type declarations for React, we first need to check if they exist&lt;/strong&gt; – the majority of type declaration files are created by the community rather than the library authors, but most popular libraries are covered.&lt;/p&gt;

&lt;p&gt;The type declaration system has been through several iterations of tooling (first &lt;code&gt;tsd&lt;/code&gt;, then &lt;code&gt;typings&lt;/code&gt;) but is now moving to be purely &lt;code&gt;npm&lt;/code&gt; based. The original definitions live in the massive &lt;a href=&#34;https://github.com/DefinitelyTyped/DefinitelyTyped&#34;&gt;DefinitelyTyped Github repo&lt;/a&gt;, and are automatically synced to &lt;code&gt;npm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It seems that the only way to check if a package has type declarations in &lt;code&gt;npm&lt;/code&gt; is to search at &lt;a href=&#34;http://microsoft.github.io/TypeSearch/&#34;&gt;http://microsoft.github.io/TypeSearch/&lt;/a&gt; – type in &lt;code&gt;react&lt;/code&gt;, and indeed it does have a definition, hosted on &lt;code&gt;npm&lt;/code&gt; at &lt;a href=&#34;https://www.npmjs.com/package/@types/react&#34;&gt;https://www.npmjs.com/package/@types/react&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Previous iterations of the typing system had the ability to search from the command line, which is preferable in most cases, so hopefully someone will fill this gap for &lt;code&gt;npm&lt;/code&gt;-based type declarations – for now, you could install &lt;code&gt;typings&lt;/code&gt; (&lt;a href=&#34;https://github.com/typings/typings&#34;&gt;https://github.com/typings/typings&lt;/a&gt;) and use that to search DefinitelyTyped, or just use Google/GitHub/npm search.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Now we know the React type declarations exist at &lt;code&gt;@types/react&lt;/code&gt;, &lt;strong&gt;we can install them with &lt;code&gt;npm&lt;/code&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm i -D @types/react
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And now both &lt;code&gt;tsc&lt;/code&gt; and your editor should be happy with the React import. At this point, you should also be able to get a sense for the autocompletion Typescript enables, by typing &lt;code&gt;React.&lt;/code&gt; somewhere in your &lt;code&gt;index.tsx&lt;/code&gt; file and seeing the autocomplete dropdown with the correct options.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;We also need the &lt;code&gt;react-dom&lt;/code&gt; package&lt;/strong&gt;, which can be installed in the same way:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm i -S react-dom
npm i -D @types/react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To my mind, it makes sense for type declarations to be in &lt;code&gt;devDependencies&lt;/code&gt; (so &lt;code&gt;npm -D&lt;/code&gt; rather than &lt;code&gt;npm -S&lt;/code&gt;). Also note that the structure of the type declaration package name is always &lt;code&gt;@types/&amp;lt;npm_package_name&amp;gt;&lt;/code&gt;, so you could take advantage of this naming scheme and try installing types for a given package based on its name rather than using the search in most cases.&lt;/p&gt;

&lt;p&gt;One other thing worth noting is that the current version of the React typings is &lt;code&gt;0.14&lt;/code&gt; whereas we are using React &lt;code&gt;0.15&lt;/code&gt; – this is one downside of type declarations being maintained by the community, but in most cases it doesn&amp;rsquo;t present too much of a problem (as long as the API hasn&amp;rsquo;t changed much). It is of course possible to put in a PR to DefinitelyTyped to fix the type defs if they are out of sync (or to augment them with local type declaration &amp;ldquo;overrides&amp;rdquo;, or in extreme cases, to import the module without any typings – more on that later).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&#34;setting-up-webpack-and-babel&#34;&gt;Setting up Webpack and Babel&lt;/h1&gt;

&lt;p&gt;As mentioned above, we&amp;rsquo;re using Babel to transpile to ES6 output from Typescript so we can take advantage of the Babel plugin ecosystem, and Webpack as our module bundler; so we need to setup Webpack to invoke the Typescript loader and then pass the output to Babel. I won&amp;rsquo;t go into too much detail on the Webpack setup as it&amp;rsquo;s a huge topic in itself!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install Webpack itself&lt;/strong&gt;, and the handy notifier plugin which will notify you of the build status with your system notifier (especially useful with Typescript as the code will be compiled every time you save a file, so this surfaces compile errors much quicker than watching the terminal):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm i -D webpack webpack-notifier
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install the Webpack Typescript loader&lt;/strong&gt;, so Webpack can handle compiling Typescript as part of the bundling process:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm i -D ts-loader
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install Babel itself, the Babel Webpack loader, and a few presets&lt;/strong&gt; so it can understand the ES6 and JSX code that the Typescript compiler will output (I&amp;rsquo;m not sure if &lt;code&gt;stage-0&lt;/code&gt; is strictly necessary, &lt;code&gt;stage-3&lt;/code&gt; might be enough for &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; support):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;npm i -D babel-core babel-loader babel-preset-es2015 babel-preset-react babel-preset-stage-0
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a &lt;code&gt;.babelrc&lt;/code&gt; file&lt;/strong&gt; in the project root to tell Babel to use the presets we just installed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{
  &amp;quot;presets&amp;quot;: [&amp;quot;es2015&amp;quot;, &amp;quot;react&amp;quot;, &amp;quot;stage-0&amp;quot;]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create a Webpack config&lt;/strong&gt; in &lt;code&gt;config/webpack.config.js&lt;/code&gt; to tell Webpack how to build and bundle the project. I&amp;rsquo;ve added comments explaining what is going on inline:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-javascript&#34;&gt;var webpack = require(&#39;webpack&#39;);
var path = require(&#39;path&#39;);
var WebpackNotifierPlugin = require(&#39;webpack-notifier&#39;);

module.exports = {
  devtool: &#39;eval&#39;,
  // This will be our app&#39;s entry point (webpack will look for it in the &#39;src&#39; directory due to the modulesDirectory setting below). Feel free to change as desired.
  entry: [
    &#39;index.tsx&#39;
  ],
  // Output the bundled JS to dist/app.js
  output: {
    filename: &#39;app.js&#39;,
    path: path.resolve(&#39;dist&#39;)
  },
  resolve: {
    // Look for modules in .ts(x) files first, then .js(x)
    extensions: [&#39;&#39;, &#39;.ts&#39;, &#39;.tsx&#39;, &#39;.js&#39;, &#39;.jsx&#39;],
    // Add &#39;src&#39; to our modulesDirectories, as all our app code will live in there, so Webpack should look in there for modules
    modulesDirectories: [&#39;src&#39;, &#39;node_modules&#39;],
  },
  module: {
    loaders: [
      // .ts(x) files should first pass through the Typescript loader, and then through babel
      { test: /\.tsx?$/, loaders: [&#39;babel&#39;, &#39;ts-loader&#39;] }
    ]
  },
  plugins: [
    // Set up the notifier plugin - you can remove this (or set alwaysNotify false) if desired
    new WebpackNotifierPlugin({ alwaysNotify: true }),
  ]
};
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Add a script to invoke Webpack&lt;/strong&gt; when you run &lt;code&gt;npm run build&lt;/code&gt; to your &lt;code&gt;package.json&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;quot;scripts&amp;quot;: {
  &amp;quot;build&amp;quot;: &amp;quot;webpack --config config/webpack.config.js&amp;quot;
},
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;You should now be able to &lt;strong&gt;build the project&lt;/strong&gt; with Webpack:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and see that it has created an output file at &lt;code&gt;dist/app.js&lt;/code&gt; (you probably want to &lt;code&gt;gitignore&lt;/code&gt; the &lt;code&gt;dist&lt;/code&gt; directory).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&#34;adding-hot-module-reloading&#34;&gt;Adding hot module reloading&lt;/h1&gt;

&lt;p&gt;Nearly there! All that remains is to add hot module reloading to the project. In reality, this is completely optional, but I find it invaluable for working on a React project, so would consider it part of any project&amp;rsquo;s setup. It&amp;rsquo;s also not really Typescript-specific, but there aren&amp;rsquo;t many good guides out there for setting up the latest version of HMR, and setting it up gives us a chance to see a couple more tips on working with Typescript.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create an &lt;code&gt;index.html&lt;/code&gt; file&lt;/strong&gt; which will load the bundled JS and give React somewhere to mount the application to. Create the file in the project root with the following contents:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang=&amp;quot;en&amp;quot;&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset=&amp;quot;UTF-8&amp;quot;&amp;gt;
  &amp;lt;title&amp;gt;My Typescript App&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;div id=&amp;quot;app&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;script src=&amp;quot;dist/app.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Next, &lt;strong&gt;install the &lt;code&gt;react-hot-loader&lt;/code&gt; &lt;code&gt;npm&lt;/code&gt; package&lt;/strong&gt;. We are using the latest 3.0.0 beta version, as it supports stateless functional components.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm i -D react-hot-loader@3.0.0-beta.2
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Add the &lt;code&gt;react-hot-loader&lt;/code&gt; plugin to &lt;code&gt;.babelrc&lt;/code&gt;&lt;/strong&gt;, so it now contains:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{
  &amp;quot;presets&amp;quot;: [&amp;quot;es2015&amp;quot;, &amp;quot;react&amp;quot;, &amp;quot;stage-0&amp;quot;],
  &amp;quot;plugins&amp;quot;: [&amp;quot;react-hot-loader/babel&amp;quot;]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Install the &lt;code&gt;webpack-dev-server&lt;/code&gt; &lt;code&gt;npm&lt;/code&gt; package&lt;/strong&gt;, to allow us to setup a dev server to work with the hot module reloading:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm i -D webpack-dev-server
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create the dev server&lt;/strong&gt; in a new file in your project root, &lt;code&gt;server.js&lt;/code&gt;, with the following contents (based on &lt;a href=&#34;https://github.com/gaearon/react-hot-boilerplate/blob/next/server.js):&#34;&gt;https://github.com/gaearon/react-hot-boilerplate/blob/next/server.js):&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var webpack = require(&#39;webpack&#39;);
var WebpackDevServer = require(&#39;webpack-dev-server&#39;);
var config = require(&#39;./config/webpack.config&#39;);

new WebpackDevServer(webpack(config), {
  publicPath: config.output.publicPath,
  hot: true,
  historyApiFallback: true
}).listen(3000, &#39;localhost&#39;, function (err, result) {
  if (err) {
    return console.log(err);
  }

  console.log(&#39;Listening at http://localhost:3000/&#39;);
});
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modify &lt;code&gt;config/webpack.config.js&lt;/code&gt; to enable Webpack support for hot module reloading&lt;/strong&gt;, so it now contains the following (explanatory comments inline):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var webpack = require(&#39;webpack&#39;);
var path = require(&#39;path&#39;);
var WebpackNotifierPlugin = require(&#39;webpack-notifier&#39;);

module.exports = {
  devtool: &#39;eval&#39;,
  entry: [
    // Add the react hot loader entry point - in reality, you only want this in your dev Webpack config
    &#39;react-hot-loader/patch&#39;,
    &#39;webpack-dev-server/client?http://localhost:3000&#39;,
    &#39;webpack/hot/only-dev-server&#39;,
    &#39;index.tsx&#39;
  ],
  output: {
    filename: &#39;app.js&#39;,
    publicPath: &#39;/dist&#39;,
    path: path.resolve(&#39;dist&#39;)
  },
  resolve: {
    extensions: [&#39;&#39;, &#39;.ts&#39;, &#39;.tsx&#39;, &#39;.js&#39;, &#39;.jsx&#39;],
    modulesDirectories: [&#39;src&#39;, &#39;node_modules&#39;],
  },
  module: {
    loaders: [
      { test: /\.tsx?$/, loaders: [&#39;babel&#39;, &#39;ts-loader&#39;] }
    ]
  },
  plugins: [
    // Add the Webpack HMR plugin so it will notify the browser when the app code changes
    new webpack.HotModuleReplacementPlugin(),
    new WebpackNotifierPlugin({ alwaysNotify: true }),
  ]
};
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Add a script to start the dev server&lt;/strong&gt; with &lt;code&gt;npm start&lt;/code&gt; to your &lt;code&gt;package.json&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;quot;scripts&amp;quot;: {
  &amp;quot;build&amp;quot;: &amp;quot;webpack --config config/webpack.config.js&amp;quot;,
  &amp;quot;start&amp;quot;: &amp;quot;node server.js&amp;quot;
},
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Finally, we need to &lt;strong&gt;create a skeleton app set up in a suitable way for hot module reloading&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our entry point needs to be set up so that it can handle requests to hot reload the app. Open up &lt;code&gt;src/index.tsx&lt;/code&gt; and change it to the following (explanatory comments inline):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Import React and React DOM
import * as React from &#39;react&#39;;
import { render } from &#39;react-dom&#39;;
// Import the Hot Module Reloading App Container – more on why we use &#39;require&#39; below
const { AppContainer } = require(&#39;react-hot-loader&#39;);

// Import our App container (which we will create in the next step)
import App from &#39;containers/App&#39;;

// Tell Typescript that there is a global variable called module - see below
declare var module: { hot: any };

// Get the root element from the HTML
const rootEl = document.getElementById(&#39;app&#39;);

// And render our App into it, inside the HMR App ontainer which handles the hot reloading
render(
  &amp;lt;AppContainer&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/AppContainer&amp;gt;,
  rootEl
);

// Handle hot reloading requests from Webpack
if (module.hot) {
  module.hot.accept(&#39;./containers/App&#39;, () =&amp;gt; {
    // If we receive a HMR request for our App container, then reload it using require (we can&#39;t do this dynamically with import)
    const NextApp = require(&#39;./containers/App&#39;).default;

    // And render it into the root element again
    render(
      &amp;lt;AppContainer&amp;gt;
         &amp;lt;NextApp /&amp;gt;
      &amp;lt;/AppContainer&amp;gt;,
      rootEl
    );
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Effectively, this renders our app inside a special container (the &lt;code&gt;react-hot-loader&lt;/code&gt; &lt;code&gt;AppContainer&lt;/code&gt;), and then waits for Webpack to notify it of any changes to any of the app&amp;rsquo;s files (which will trigger HMR requests, which end up bubbling up to the top level parent module, &lt;code&gt;containers/App&lt;/code&gt;). When a change is detected, the whole &lt;code&gt;App&lt;/code&gt; container is reloaded and the existing instance of it in the DOM is replaced with the new, modified one.&lt;/p&gt;

&lt;p&gt;The trick here is that the &lt;code&gt;react-hot-loader&lt;/code&gt; &lt;code&gt;AppContainer&lt;/code&gt; takes care of persisting the state of components (whether local state or in Redux or similar) when it is reloaded, so in most cases, we don&amp;rsquo;t lose where we were in the app.&lt;/p&gt;

&lt;p&gt;A quick note on a couple of things:&lt;/p&gt;

&lt;h2 id=&#34;importing-modules-without-type-declarations&#34;&gt;Importing modules without type declarations&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;const { AppContainer } = require(&#39;react-hot-loader&#39;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here, we are using &lt;code&gt;require&lt;/code&gt; rather than &lt;code&gt;import&lt;/code&gt;. This is a useful trick to know, as it allows you to import an &lt;code&gt;npm&lt;/code&gt; module without requiring any type declarations.&lt;/p&gt;

&lt;p&gt;In this case, there are currently no type declarations for &lt;code&gt;react-hot-loader&lt;/code&gt;, but if we use &lt;code&gt;require&lt;/code&gt;, it is treated as being of &lt;code&gt;any&lt;/code&gt; type. This can be handy for working with modules that don&amp;rsquo;t have type declarations, particularly if you are just trying out modules to see if they are suitable and don&amp;rsquo;t want worry about typing them.&lt;/p&gt;

&lt;p&gt;If you use &lt;code&gt;const Whatever = require(&#39;whatever&#39;);&lt;/code&gt; rather than &lt;code&gt;import Whatever from &#39;whatever&#39;;&lt;/code&gt;, Typescript won&amp;rsquo;t complain about &lt;code&gt;Cannot find module&lt;/code&gt; — although it also won&amp;rsquo;t offer you any type safety when working with this module!&lt;/p&gt;

&lt;p&gt;See the next step for an important note on this – by default, Typescript doesn&amp;rsquo;t know what &lt;code&gt;require&lt;/code&gt; means (as it&amp;rsquo;s not a built-in Javascript construct).&lt;/p&gt;

&lt;h2 id=&#34;declaring-global-variables&#34;&gt;Declaring global variables&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;declare var module: { hot: any };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;declare&lt;/code&gt; is how we can tell Typescript about global variables that it doesn&amp;rsquo;t already know about. Here, we are telling it that a global variable called &lt;code&gt;module&lt;/code&gt; will exist, and it&amp;rsquo;s type will be an object, which has a key called &lt;code&gt;hot&lt;/code&gt;, who&amp;rsquo;s value is of type &lt;code&gt;any&lt;/code&gt; (which means it will not be type-checked – a bit of a cop out, and generally &lt;code&gt;any&lt;/code&gt; types should be avoided, but this is okay for our purposes here).&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;If you run &lt;code&gt;tsc&lt;/code&gt; at this point, you&amp;rsquo;ll get an error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;src/index.tsx(5,26): error TS2304: Cannot find name &#39;require&#39;.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is because Typescript doesn&amp;rsquo;t know what &lt;code&gt;require&lt;/code&gt; means. There are a few ways round this, for example installing the &lt;code&gt;node&lt;/code&gt; or &lt;code&gt;requirejs&lt;/code&gt; type declarations, but the one recommended at &lt;a href=&#34;https://github.com/TypeStrong/ts-loader#loading-other-resources-and-code-splitting&#34;&gt;https://github.com/TypeStrong/ts-loader#loading-other-resources-and-code-splitting&lt;/a&gt; which plays nicely with CSS modules is to create your own declaration for &lt;code&gt;require&lt;/code&gt; — this also gives me the opportunity to demonstrate how we can create our own local type declarations for libraries!&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s up to you where you would like to keep your local type declarations, but I would suggest a top-level directory named &lt;code&gt;type-declarations&lt;/code&gt;. If you are using the default &lt;code&gt;exclude&lt;/code&gt; pattern for &lt;code&gt;tsconfig.json&lt;/code&gt;, any &lt;code&gt;*.ts&lt;/code&gt; files in your entire project except those in &lt;code&gt;node_modules&lt;/code&gt; will automatically be included in the compilation, so we don&amp;rsquo;t need to explicitly tell Typescript where we have put the declarations.&lt;/p&gt;

&lt;p&gt;A type declaration file has the extension &lt;code&gt;.d.ts&lt;/code&gt;, and can declare types either globally or inside a named module scope (which is how most third party library definitions are written, so the types only come into scope when the module is imported). Details of how type declarations work is beyond the scope of this guide, but for now &lt;strong&gt;create a file named &lt;code&gt;require.d.ts&lt;/code&gt; in a directory called &lt;code&gt;type-declarations&lt;/code&gt; in the project root&lt;/strong&gt; with the following contents:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;declare var require: {
    (path: string): any;
    &amp;lt;T&amp;gt;(path: string): T;
    (paths: string[], callback: (...modules: any[]) =&amp;gt; void): void;
    ensure: (paths: string[], callback: (require: &amp;lt;T&amp;gt;(path: string) =&amp;gt; T) =&amp;gt; void) =&amp;gt; void;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Run &lt;code&gt;tsc&lt;/code&gt; again, and the warning should be gone (with just one about &lt;code&gt;containers/App&lt;/code&gt; being missing remaining) – we&amp;rsquo;ve told Typescript that calling &lt;code&gt;require&lt;/code&gt; with a single string argument will return a variable of type &lt;code&gt;any&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This technique of creating local type declarations can also be useful if a libraries&amp;rsquo; declarations are out of date or inaccurate – you can add exported variables or additional properties to existing interfaces like so (although really it&amp;rsquo;s best to submit these changes back to DefinitelyTyped if time allows):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;declare module &amp;quot;redux-form&amp;quot; {
    export var change: any

    export interface ReduxFormConfig {
        alwaysAsyncValidate?: boolean
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;The final step: we need to create our actual &lt;code&gt;App&lt;/code&gt; container component. We&amp;rsquo;ll just create a placeholder for now, but in reality this would be the root component of your app (e.g. where your &lt;code&gt;&amp;lt;Router&amp;gt;&lt;/code&gt; lives).&lt;/p&gt;

&lt;p&gt;Create a new directory, &lt;code&gt;src/containers&lt;/code&gt;, and create a new file in there, &lt;code&gt;App.tsx&lt;/code&gt;, with the following contents:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import * as React from &#39;react&#39;

export default () =&amp;gt; &amp;lt;div&amp;gt;Hello world&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you should be able to start your dev server:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and go to &lt;a href=&#34;http://127.0.0.1:3000&#34;&gt;http://127.0.0.1:3000&lt;/a&gt; to see the &amp;ldquo;Hello World&amp;rdquo; message! Update the &lt;code&gt;App.tsx&lt;/code&gt; file to say something else, and you should see the app hot reload.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&#34;next-steps&#34;&gt;Next steps&lt;/h1&gt;

&lt;p&gt;That&amp;rsquo;s basically it in terms of project setup. As mentioned at the start, the complete template is available at &lt;a href=&#34;https://github.com/tomduncalf/typescript-react-template&#34;&gt;https://github.com/tomduncalf/typescript-react-template&lt;/a&gt; – I think it&amp;rsquo;s helpful to go through each step and understand why it is required the first time, but in future, you can use the template you&amp;rsquo;ve created as a starting point for new projects.&lt;/p&gt;

&lt;p&gt;In terms of next steps, it&amp;rsquo;s really just a case of building your app as usual, but taking advantage of Typescript&amp;rsquo;s type checking and editor integration. The only real pain point is likely to be working with type declarations for third party libraries, but using &lt;code&gt;npm&lt;/code&gt; for the typings has made this easier, and there is always the option to import the library using &lt;code&gt;require&lt;/code&gt; and bypassing the need for type declarations initially.&lt;/p&gt;

&lt;p&gt;I intend to write more about working with Typescript and React soon, but hopefully this is a useful start – any questions, comments or feedback is very welcome either via the comments, or via Twitter or email.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>