<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Byron Mitchell on Medium]]></title>
        <description><![CDATA[Stories by Byron Mitchell on Medium]]></description>
        <link>https://medium.com/@byro.mitchell?source=rss-3a67190c6345------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GQU69jms2arz9pKa.jpg</url>
            <title>Stories by Byron Mitchell on Medium</title>
            <link>https://medium.com/@byro.mitchell?source=rss-3a67190c6345------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 23:51:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@byro.mitchell/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Adding TypeScript Support to a Babel React App]]></title>
            <link>https://medium.com/priceline-labs/adding-typescript-support-to-a-babel-react-app-b78214f38ba9?source=rss-3a67190c6345------2</link>
            <guid isPermaLink="false">https://medium.com/p/b78214f38ba9</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[typescript-with-react]]></category>
            <dc:creator><![CDATA[Byron Mitchell]]></dc:creator>
            <pubDate>Wed, 06 Nov 2019 19:04:46 GMT</pubDate>
            <atom:updated>2019-11-06T19:04:46.027Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="It’s me as a dog overcoming all the roadblocks." src="https://cdn-images-1.medium.com/max/1024/1*2zzJ0vcm1wqJI2KspOX1gg.jpeg" /></figure><p>In this post, I’m going to walk through all of the steps we used to upgrade three of our applications so that we could start mixing TS files in with our JS files. Until this past year it was very difficult to migrate to TypeScript in an existing app because if you were using something like <strong>Babel</strong> you would have to rip it out and replace it with the <strong>TypeScript</strong> compiler. Fortunately, the folks on the TypeScript team and the Babel teams worked together to create a plugin (<strong>@babel/preset-typescript)</strong> for <strong>Babel</strong> that would handle the transpilation of TypeScript into JavaScript, meaning there is no longer a need to replace <strong>Babel</strong>.</p><p>Unfortunately, adding TypeScript support to our apps without making any concessions like broken linting or unit tests was a little more involved than simply adding the <strong>babel-typescript</strong> plugin, so this post will go through all of the roadblocks I encountered and how I resolved them. If you use this as a guide, you should have an app at the end that is fully linted and supports mixed <strong>JavaScript</strong> and<strong> TypeScript</strong>.</p><p><strong>Roadblock 1: We Were Still Using Babel 6</strong></p><p>All of the apps that my team work on used <strong>Babel</strong> <strong>6</strong>. Sadly, <strong>@babel/preset-typescript</strong> requires <strong>Babel</strong> <strong>7</strong>. If you already have <strong>Babel</strong> <strong>7</strong> you can skip this whole section. If you don’t, you’ll have to either manually upgrade to <strong>Babel 7</strong> by updating all dependencies in your <strong>package.json </strong>or you can use the amazing <strong>babel/babel-upgrade </strong>CLI tool to handle most of it for you. Below are the steps I took on all of our apps:</p><ol><li>Install<strong> </strong><strong>babel/babel-upgrade </strong>globally with <strong>npm install -g babel/babel-upgrade</strong>. This is a tool that will make switching from<strong> Babel 6 </strong>to<strong> Babel 7 </strong>a relatively painless process. It misses a few important things like updating references to <strong>babel-polyfill</strong> but it was a huge time saver.</li><li>Use the babel upgrade tool in the project directory with <strong>npx babel-upgrade --write. </strong>This will make most of the necessary updates to <strong>.babelrc</strong> and <strong>package.json</strong> based on what you already have in there.</li><li><strong>npm install </strong>in your project’s root directory<strong> </strong>to install the new packages and update <strong>package-lock.json</strong>.</li><li>Optional (but might be necessary): <strong>babel-upgrade</strong> adds all necessary packages with <strong>‘7.0.0’ </strong>as the version. You can manually update the modules to a more recent version if you’re having issues. We ended up updating all of the packages to <strong>‘7.4.4’</strong>.</li><li>Manually update any references to <strong>babel-polyfill</strong> to <strong>@babel/polyfill</strong>. These will typically be in the <strong>webpack</strong> configuration files.</li><li>Add <strong>browserslist</strong> to <strong>package.json</strong>. This will reduce the size of the bundle by only supporting the browsers in the list.</li></ol><pre>“browserslist”: <br> “last 2 chrome versions”,<br> “last 2 safari versions”,<br> “last 2 firefox versions”,<br> “ie &gt;= 11”,<br> “last 2 edge versions”,<br> “&gt; 0.5%”<br>]</pre><p><strong>Roadblock 2: We Needed to Add the Babel-TypeScript Plugin</strong></p><p>Now that we had <strong>Babel 7</strong> in our app, it was time to add the plugin that <strong>Babel</strong> uses to convert TypeScript code to JavaScript. These are essentially the same instructions that Microsoft included in their announcement blog <a href="https://devblogs.microsoft.com/typescript/typescript-and-babel-7/">here</a> with a few modifications to download some types and a slightly modified <strong>tsconfig.json</strong> file.</p><ol><li><strong>npm install --save-dev @babel/preset-typescript </strong>to install the babel Typescript plugin. This is what allows us to use Typescript without having to switch to using the Typescript compiler. Install <strong>npm install --save-dev @types/jest @types/react-dom @types/react</strong> which are type definitions that you’ll need for an application that makes use of <strong>React</strong>.</li><li>Add <strong>@babel/preset-typescript</strong> to <strong>.babelrc</strong> under <strong>presets</strong> as the last item.</li><li>Add a <strong>tsconfig.json </strong>file at the root of the project.</li></ol><pre>&quot;compilerOptions&quot;: {<br>  “baseUrl”: “src”,<br>  // Optional, but makes it so you don’t have to use relative    <br>                      // paths for imports. Add as many folders as you need.<br>  “paths”: {<br>    “components”: [<br>      “./components”<br>    ]<br>  }<br>  // Target latest version of ECMAScript.<br>  &quot;target&quot;: &quot;ESNext&quot;,<br>  // Search under node_modules for non-relative imports.<br>  &quot;moduleResolution&quot;: &quot;node&quot;,<br>  &quot;jsx&quot;: &quot;react&quot;,<br>  // Don&#39;t emit; allow Babel to transform files.<br>  &quot;noEmit&quot;: true,<br>  // Import non-ES modules as default imports.<br>  &quot;esModuleInterop&quot;: true<br> },<br> &quot;include&quot;: [<br>  &quot;src&quot;<br> ]<br>}</pre><p><strong>Roadblock 3: Webpack Didn’t Know to Load Our TypeScript Files</strong></p><p>In order to be able to use the TS files without having to specify the extension in each import statement, we needed to let <strong>webpack</strong> know to also look at <strong>.ts </strong>and <strong>.tsx</strong> files when resolving the import paths. We also needed to let <strong>webpack</strong> know that <strong>Babel </strong>is going to handle the transpilation of <strong>.ts </strong>and <strong>.tsx</strong> files. Both of the following changes had to be made in the <strong>webpack</strong> configuration files.</p><ol><li>Add <strong>extensions</strong> option to the <strong>webpack</strong> <strong>resolve</strong> options object. E.g. <strong>extensions: [‘.ts’, ‘.tsx’, ‘.js’, ‘.json’]</strong> . This allows us to import <strong>.ts</strong> files without having to specify the <strong>.ts</strong> and it will prioritize the TypeScript files over the JavaScript ones</li><li><strong>.babel-loader</strong> module configuration needs to be updated from something like <strong>test: /\.js$/</strong> to <strong>test: [/\.jsx?$/, /\.tsx?$/]</strong></li></ol><p><strong>Roadblock 4: Jest Didn’t Know To Test Our TypeScript files</strong></p><p>We also wanted to be able to test the new TypeScript files and collect test coverage information for them. In order to facilitate that, we had to install the <strong>babel-jest </strong>module and make several modifications to our <strong>jest.config.js</strong> file.</p><ol><li>Make sure the <strong>babel-jest</strong> module is installed <strong>npm install --save-dev babel-jest </strong>and add the <strong>babel-jest</strong> transform if it’s not already present. <strong>transform: {‘^.+\\.(js|jsx|ts|tsx)$’: ‘babel-jest’}</strong></li><li>Update the <strong>collectCoverageFrom</strong> option to include <strong>ts</strong> and <strong>tsx</strong> files. E.g. <strong>‘src/**/*.js’</strong> to <strong>‘src/**/*.{js,jsx,ts,tsx}’</strong></li><li>Update <strong>testMatch</strong> option to include <strong>ts</strong> and <strong>tsx</strong> files. E.g. <strong>‘**/__tests__/**/*.js?(x)’</strong> to <strong>‘**/__tests__/**/*.(js|ts)?(x)’</strong></li><li>Add or modify <strong>moduleFileExtensions</strong> to include <strong>ts</strong> and <strong>tsx</strong> files:</li></ol><pre>moduleFileExtensions: [<br>  &#39;ts&#39;,<br>  &#39;tsx&#39;,<br>  &#39;js&#39;,<br>  &#39;jsx&#39;,<br>  &#39;json&#39;,<br>  &#39;node&#39;<br> ]</pre><p><strong>Roadblock 5: Our Linter Didn’t Know How To Lint Our TypeScript Files</strong></p><p>Prior to adding in mixed TypeScript support we were using StandardJS. StandardJS is great, but it’s only for JavaScript. I made several attempts to get StandardJS to work with our new mixed JS/TS application including trying to use official instructions that StandardJS folks recommend using. However, I opted in the end to just switch back to <strong>eslint (v5) </strong>and used the <strong>eslint-config-standard </strong>and <strong>eslint-config-standard-react </strong>rules presets. Newer versions of <strong>eslint</strong> support an “override” configuration option that lets you specify a different parser for specific file types. So we opted to use <strong>babel-eslint </strong>as the default parser for all of our JS files and <strong>@typescript-eslint/parser </strong>for all of our TS files.</p><p>Unfortunately, you can’t use “extends” in “overrides” yet, so we had to rename our <strong>.eslintrc </strong>file to <strong>.eslintrc.js</strong> so that we could require the list of recommended TypeScript rules and programmatically add them as outlined in <a href="http://www.thedreaming.org/2019/03/27/eslint-with-typescript/">this post</a>. I’m hoping that in the future “extends” will be supported. Here are the steps I took to enable JS/TS mixed linting:</p><ol><li>Add <strong>typescript</strong>, <strong>@typescript-eslint/eslint-plugin</strong>, and <strong>@typescript-eslint/parser </strong>modules with <strong>npm install --save-dev typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser babel-eslint</strong>. Even though we’re using <strong>Babel</strong> to compile our Typescript to regular JavaScript, eslint requires the module to be installed in order to parse our TS files.</li><li>I added a couple of scripts to the ”scripts” object in <strong>package.json. </strong>Since <strong>eslint</strong> only runs on JavaScript by default I had to pass a few — ext options<strong>.</strong></li></ol><pre>&quot;lint&quot;: &quot;eslint --ext .js --ext .jsx --ext .ts --ext .tsx src test config&quot;,<br>&quot;format&quot;: &quot;eslint --ext .js --ext .jsx --ext .ts --ext .tsx src test config --fix&quot;</pre><p>3. Below is the my full <strong>.eslintrc.js </strong>file.</p><pre>// Import the recommended rules for TypeScript<br>const typescriptEslintRecommended = require(&#39;<a href="http://twitter.com/typescript">@typescript</a>-eslint/eslint-plugin&#39;).configs.recommended</pre><pre>module.exports = {<br>  parser: &#39;babel-eslint&#39;,<br>  parserOptions: {<br>    ecmaVersion: 8,<br>    sourceType: &#39;module&#39;,<br>    ecmaFeatures: {<br>      jsx: true,<br>      modules: true,<br>      experimentalObjectRestSpread: true<br>    }<br>  },<br>  extends: [<br>    &#39;standard&#39;,<br>    &#39;standard-react&#39;<br>  ],<br>  plugins: [<br>    &#39;babel&#39;,<br>    &#39;jest&#39;<br>  ],<br>  env: {<br>    &#39;jest/globals&#39;: true<br>  },<br>  rules: {<br>    &#39;no-console&#39;: [<br>      &#39;warn&#39;<br>    ],<br>    &#39;jest/no-disabled-tests&#39;: &#39;warn&#39;,<br>    &#39;jest/no-focused-tests&#39;: &#39;error&#39;,<br>    &#39;jest/no-identical-title&#39;: &#39;error&#39;,<br>    &#39;jest/valid-expect&#39;: &#39;error&#39;,<br>    &#39;object-curly-spacing&#39;: &#39;off&#39;,<br>    &#39;no-unused-vars&#39;: &#39;off&#39;,<br>    &quot;react/prop-types&quot;: [2, { ignore: [&#39;children&#39;] }]<br>  },<br>  overrides: [<br>    {<br>      files: [<br>        &#39;**/*.ts&#39;,<br>        &#39;**/*.tsx&#39;<br>      ],<br>      parser: &#39;<a href="http://twitter.com/typescript">@typescript</a>-eslint/parser&#39;,<br>      parserOptions: {<br>        project: &#39;./tsconfig.json&#39;<br>      },<br>      plugins: [ &#39;<a href="http://twitter.com/typescript">@typescript</a>-eslint&#39; ],<br>      rules: Object.assign(typescriptEslintRecommended.rules, {<br>        &#39;<a href="http://twitter.com/typescript">@typescript</a>-eslint/no-unused-vars&#39;: &#39;off&#39;,<br>        &#39;<a href="http://twitter.com/typescript">@typescript</a>-eslint/member-delimiter-style&#39;: &#39;none&#39;,<br>        &#39;<a href="http://twitter.com/typescript">@typescript</a>-eslint/explicit-function-return-type&#39;: &#39;off&#39;,<br>        &#39;react/prop-types&#39;: &#39;off&#39;<br>      })<br>    }<br>  ],<br>  globals: {<br>    __DEV__: true<br>  }<br>}</pre><p><strong>Roadblock 6: Eslint Doesn’t Check types</strong></p><p>The linter was set up but there were lots of coding mistakes it didn’t account for, namely TypeScript type safety. Fortunately, since we installed <strong>typescript </strong>as a dev dependency earlier, we were able to make use of the <strong>tsc </strong>command. This command runs type checking on all TypeScript files. So to <strong>package.json </strong>I just added a new script to ”scripts”:</p><pre>&quot;check-types&quot;: &quot;tsc --skipLibCheck&quot;</pre><p><strong>Roadblock 7: I Really Didn’t Want to Have To Manually Run Linting and Type Checking</strong></p><p>Having <strong>eslint </strong>and <strong>tsc </strong>to check the code is nice, but I wanted to incorporate it into the testing process for our projects. So I updated the ”test” script in ”scripts” in <strong>package.json</strong> to do linting and type checking as part of the testing process:</p><pre>&quot;test&quot;: &quot;npm run format &amp;&amp; npm run check-types &amp;&amp; NODE_ENV=test jest --no-cache --silent --coverage&quot;,</pre><p><strong>Conclusion</strong></p><p>After getting through all those roadblocks I had an app that I could start gradually converting to TypeScript. Whenever I’m working on a new feature I try to convert all of the JavaScript files I work with into TypeScript. I just rename the file and then, thanks to having <strong>eslint</strong> set up, I fix all the red squiggles I see. I hope that this post helps anyone else that is struggling with these roadblocks. Thanks for reading!</p><p><strong>Resources</strong></p><p><a href="https://iamturns.com/typescript-babel/">https://iamturns.com/typescript-babel/</a> — This is the page I first found out about babel/preset-typescript.</p><p><a href="https://devblogs.microsoft.com/typescript/typescript-and-babel-7/">https://devblogs.microsoft.com/typescript/typescript-and-babel-7/</a> — The original blog post announcing the project.</p><p><a href="https://babeljs.io/docs/en/babel-preset-typescript">https://babeljs.io/docs/en/babel-preset-typescript</a> — Docs for babel-typescript.</p><p><a href="http://www.thedreaming.org/2019/03/27/eslint-with-typescript/">http://www.thedreaming.org/2019/03/27/eslint-with-typescript/</a> — Guide to using eslint with typescript</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b78214f38ba9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/priceline-labs/adding-typescript-support-to-a-babel-react-app-b78214f38ba9">Adding TypeScript Support to a Babel React App</a> was originally published in <a href="https://medium.com/priceline-labs">priceline labs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>