<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>reactjs Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/reactjs/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/reactjs/</link>
	<description>Software development blog</description>
	<lastBuildDate>Mon, 28 Jun 2021 04:41:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.yumasoft.pl/wp-content/uploads/2021/05/cropped-yumasoft_icon_transparent-32x32.png</url>
	<title>reactjs Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/reactjs/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Migrating JavaScript React App to TypeScript</title>
		<link>https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/</link>
					<comments>https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Mon, 28 Jun 2021 07:15:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript to typescript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[react]]></category>
		<category><![CDATA[reactjs]]></category>
		<category><![CDATA[ts]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=342</guid>

					<description><![CDATA[<p>In one of our biggest projects, we develop and maintain a React web application. Last year, we decided to migrate it to TypeScript. How did it go? How did we migrate JavaScript to TypeScript? Was it worth it? What struggles we met and are we still having any issues? I&#8217;ll try to address these questions&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">Migrating JavaScript React App to TypeScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In one of our biggest projects, we develop and maintain a React web application. Last year, we decided to migrate it to TypeScript. How did it go? How did we migrate JavaScript to TypeScript? Was it worth it? What struggles we met and are we still having any issues? I&#8217;ll try to address these questions in this article.</p>



<p><em>Disclaimer: I&#8217;m not describing the migration process step-by-step or TypeScript itself in this article. You can find many resources on that online, including <a href="https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html">official TS docs</a>.</em></p>



<span id="more-342"></span>



<h2 class="wp-block-heading">JavaScript to TypeScript migration strategy</h2>



<p>If you search online, you will find many possible strategies of migrating JavaScript to TypeScript. We considered two of them:</p>



<ul class="wp-block-list"><li>converting the whole JavaScript codebase to TypeScript at once</li><li>starting to use TypeScript compiler and migrate step-by-step.</li></ul>



<p>After a short discussion, we decided to go with the <strong>step-by-step migration strategy</strong>. Why? First, it&#8217;s less time-consuming. Today, we know that it would kill us if we decided to convert all our JS codebase to TS at once. It would also freeze our codebase for a while &#8211; until every <code>.JS(X)</code> file is renamed to <code>.TS(X)</code> and all compile errors are fixed.</p>



<p>Thanks to the fact that TypeScript is a superset of JavaScript, <code>.JS(X)</code> and <code>.TS(X)</code> files can co-exist even if you switch to use TypeScript compiler. That&#8217;s because <strong>every JavaScript is also a valid TypeScript code</strong> and TS compiler can handle it.</p>



<p>That&#8217;s very comfortable. Just think about that &#8211; <strong>you can switch your project to TypeScript without actually having to change any code</strong>. We decided to immediately re-write a few of our recently-added functionalities to TypeScript and convert the rest gradually.</p>



<h2 class="wp-block-heading">What TypeScript gave us</h2>



<p>Before I tell you about the struggles, I want to really <strong>encourage you to migrate JavaScript to TypeScript</strong>. After 1 year since we switched to TypeScript, I&#8217;m telling you <strong>it&#8217;s the best thing you can do to your web app</strong> ?</p>



<p>Let&#8217;s see what you&#8217;ll gain if you decide to migrate JavaScript to TypeScript.</p>



<h3 class="wp-block-heading">Types in new code</h3>



<p>As soon as you switch your compiler to <em>ts-loader</em>, you can start creating files with TypeScript extensions (<code>.TS</code>, <code>.TSX</code>). It means that all your new code can now leverage types:</p>



<script src="https://gist.github.com/dsibinski/3e82afea66834d58d9c146213978af83.js"></script>



<p>Enjoy ?</p>



<h3 class="wp-block-heading">Typing information for existing libraries</h3>



<p>Have you been wondering what about the external libraries you are already using? How to get types for them? I was thinking exactly about this before migration.</p>



<p>It turns out that after you migrate JavaScript to TypeScript, most of the <code>npm</code> packages you use already have built-in typing information. You can check it on your package&#8217;s <code>npm</code> page:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img fetchpriority="high" decoding="async" width="804" height="158" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon.png" alt="" class="wp-image-350" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon.png 804w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon-300x59.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/npm_typescript_icon-768x151.png 768w" sizes="(max-width: 804px) 100vw, 804px" /></figure></div>



<p>If it doesn&#8217;t, you can try installing <code>npm</code> package called &#8220;@types/your_library_name&#8221;, e.g. <code>npm i @types/backbone</code>.</p>



<p>As a last resort, you can <a href="https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html">create the declaration files yourself</a>.</p>



<h3 class="wp-block-heading">JavaScript-TypeScript coexistence</h3>



<p>A huge benefit of TypeScript is that JavaScript code can co-exist with it. If you are like we were a year ago, with a lot of legacy JavaScript code and are afraid of switching to TS &#8211; don&#8217;t be. TypeScript makes it very easy to maintain your JS codebase or even mix it altogether.</p>



<p>There are no issues in using JavaScript code in TypeScript and otherwise. You can even <a href="https://blog.yumasoft.pl/2021/06/how-to-use-javascript-component-in-react-component/">use JS components in TypeScript</a>. We used to have a <code>services.js</code> file where we kept &#8220;services&#8221; exported as follows:</p>



<script src="https://gist.github.com/dsibinski/91250a9da08780129b7dd4e7b056f62c.js"></script>



<p>After migrating it to TypeScript (meaning renaming to <code>services.ts</code>) it turns out that these two definitions can co-exist, even in the same file:</p>



<script src="https://gist.github.com/dsibinski/13b1ba20c4c7fb03171550cbd1b04f0c.js"></script>



<p>As you can see, we added typing information to the legacy <code>StockService</code> as well. That&#8217;s because we have <code>noImplicitAny</code> enabled in our <a href="https://www.typescriptlang.org/tsconfig/#noImplicitAny">TypeScript configuration</a>. It doesn&#8217;t allow leaving any variables which are of <code>any</code> type. In any case, if you don&#8217;t know what type to use for a given variable/function, you can always type it explicitly as <code>any</code>, while moving the new code to fully-typed equivalents. That&#8217;s the power of TypeScript ?</p>



<h3 class="wp-block-heading">Compile errors</h3>



<p>I should have started with that! It might be obvious, but not for people who only worked with JavaScript on the frontend before ? Sure, you could get suggestions and warnings with <a href="https://eslint.org/">ESLint</a>, but that&#8217;s not the same as really enforced types checking on compilation level:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="804" height="141" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error.png" alt="" class="wp-image-358" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error.png 804w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error-300x53.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/TypeScript_webpack_compilation_error-768x135.png 768w" sizes="(max-width: 804px) 100vw, 804px" /></figure></div>



<h3 class="wp-block-heading">Many TS compiler options</h3>



<p>TypeScript compiler has many useful <a href="https://www.typescriptlang.org/tsconfig#noImplicitAny">configuration options</a>. If you want to support JavaScript alongside TypeScript, you should enable <code>allowJs</code>. We have already mentioned the <code>noImplicitAny</code> setting that forces you to always explicitly specify types in your code. There&#8217;s much more stuff helpful in various scenarios of JS-TS migration. We were having some issues with imports from modules without default exports &#8211; enabling <code>allowSyntheticDefaultImports</code> saved us there.</p>



<p>What I&#8217;d like to emphasize here is that TypeScript compiler is really helpful ? It allows you to set your &#8220;typing&#8221; level by enforcing less or more rules. Thanks to that, you can slowly move towards more strict type checking rules like <code>noImplicitAny</code>. You&#8217;re not forced to do that from the beginning.</p>



<h3 class="wp-block-heading">Better coding experience</h3>



<p>This is definitely the best thing about TypeScript. It just feels more right to use it than coding in plain JavaScript. I see no reason to start a new project with vanilla JS today. I&#8217;d say even more &#8211; I see no reason to not migrate your JavaScript app to TypeScript. The sooner, the better!</p>



<p>You can catch your errors earlier. No need to debug the code to see if the property you used actually exists on an object. Your IDE gives you code completion suggestions. It&#8217;s a pure pleasure ?</p>



<h2 class="wp-block-heading">Migration challenges</h2>



<p>Of course, we met some challenges during migration. I don&#8217;t call them issues, because they are all manageable. Many people have already done those things, so you can find solutions to almost any issue online. Let&#8217;s see what we had to overcome.</p>



<h3 class="wp-block-heading">webpack configuration</h3>



<p>If there&#8217;s something I don&#8217;t like about web development, it&#8217;s definitely <a href="https://webpack.js.org/">webpack.</a>.. This is a typical &#8220;fighting with machines&#8221; case. Poor documentation, lack of well-described solutions online and some specificity in every project.</p>



<p>We had problems with <code>babel-loader</code> which we used to transpile JavaScript files before switching to TypeScript. Initially, afraid of introducing a regression, we wanted to keep <code>babel-loader</code> for legacy JS files. We couldn&#8217;t make it work, so we decided to switch completely to <code>ts-loader</code> for all files (including JavaScript ones).</p>



<p>Next issue was a lack of proper source maps and problems in debug mode. The breakpoints were often not hit. After a bit of searching and trial-error sessions, we found a solution. It was necessary to modify <code>webpack.config.js</code> and set <code>devtool</code> webpack setting to <code>eval-source-map</code>. We also had to use <a href="https://www.npmjs.com/package/source-map-loader">source-map-loader</a> for JavaScript files. Finally, this webpack configuration solved the debugging problems:</p>



<script src="https://gist.github.com/dsibinski/2cb3c4c10fe63a9020508c20fa31eef3.js"></script>



<p>You need to be really patient with webpack&#8230; But if you have already worked with it, I guess you know that very well ?</p>



<h3 class="wp-block-heading">Adding types to legacy JS code</h3>



<p>Adding types information with <code>noImplicitAny</code> set to <code>true</code> is sometimes challenging. Especially if you have some bigger JS files with no typing information at all. Sometimes you need to search for function&#8217;s calls to find out of what types are its parameters. In other cases, you might need to run your app and see that at runtime ? </p>



<p>Fortunately, as we already said, TypeScript doesn&#8217;t force you doing that. You can play with <a href="https://www.typescriptlang.org/tsconfig#noImplicitAny">tsconfig</a> or use <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-6.html#suppress-errors-in-ts-files-using--ts-ignore-comments">@ts-ignore</a> as a last resort. </p>



<h3 class="wp-block-heading">Handling libraries without types</h3>



<p>Some of the libraries we used don&#8217;t provide any typing information. It makes working with them a bit difficult, especially if your TS compiler is already configured in a bit stricter mode. In such case, you might get an error similar to the following one:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img decoding="async" width="1024" height="184" src="https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-1024x184.png" alt="" class="wp-image-365" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-1024x184.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-300x54.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types-768x138.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/06/formio_missing_types.png 1062w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>Your options here are to either write the declaration files yourself, or import the library differently. You can, for instance, <code>require</code> it:</p>



<pre class="wp-block-code"><code>const FormIO = require('react-formio');</code></pre>



<p>and then use this <code>FormIO</code> variable which is typed as <code>any</code>. Again &#8211; that works because of TypeScript&#8217;s flexibility, but might not be very comfortable to live with.</p>



<h2 class="wp-block-heading">Migrate JavaScript to TypeScript &#8211; should I do it?</h2>



<p>Of course! There&#8217;s only one right answer. Don&#8217;t wait too long &#8211; start being more productive with TypeScript as soon as you can ? Believe me &#8211; you will not regret it.</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">Migrating JavaScript React App to TypeScript</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
