<?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>ts Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/ts/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/ts/</link>
	<description>Software development blog</description>
	<lastBuildDate>Wed, 27 Oct 2021 03:21:56 +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>ts Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/ts/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How Does TypeScript Types Narrowing Work?</title>
		<link>https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/</link>
					<comments>https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 27 Oct 2021 05:30:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ts]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=736</guid>

					<description><![CDATA[<p>Have you ever heard about TypeScript types narrowing? Have you ever seen such an error in your TypeScript code: Even tough, you really knew that the name property exists on the person object? How did you solve this problem? Did you cast the variable to the expected type? If the answer to at least one&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/">How Does TypeScript Types Narrowing Work?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Have you ever heard about <em>TypeScript types narrowing</em>? Have you ever seen such an error in your TypeScript code:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="604" height="118" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_missing_property_error.png" alt="TypeScript complains about &quot;name&quot; property not existing  on &quot;person&quot; object of type &quot;object&quot;" class="wp-image-746" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_missing_property_error.png 604w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_missing_property_error-300x59.png 300w" sizes="(max-width: 604px) 100vw, 604px" /></figure></div>



<p>Even tough, you <em>really knew</em> that the <code>name</code> property exists on the <code>person</code> object? How did you solve this problem? Did you cast the variable to the expected type?</p>



<p>If the answer to at least one of those questions is <em>yes,</em> you might read on. There are better ways of determining the type of local variable in TypeScript. What&#8217;s more, TS compiler does a lot of stuff for us by leveraging types narrowing. In this article, we are going to see how it works and how it can be useful ?</p>



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



<h2 class="wp-block-heading">TypeScript types narrowing &#8211; what is that?</h2>



<p>One of the <a href="https://blog.yumasoft.pl/2021/06/migrating-javascript-react-app-to-typescript/">goals of TypeScript</a> is to make writing JavaScript code better and more secure by adding the typing system. However, we don&#8217;t want the typing system to be problematic and making us to use the <em>dirty haxes</em> like all the time casting our variables explicitly, do we? ?</p>



<p>First, we need to understand that TypeScript does a lot of stuff in order to ensure we work with the most specific type as possible. Basically, if the exact type of local variable can be determined, TS compiler tries to do that.</p>



<p>Imagine such a function:</p>



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



<p>In <a href="https://www.javatpoint.com/what-is-vanilla-javascript">vanilla JS</a>, such code would of course compile. But in our case, TypeScript sees a problem:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img decoding="async" width="730" height="170" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_type_error.png" alt="TypeScript complains that &quot;lenght&quot; doesn't exist on a variable of type &quot;string&quot; or &quot;number&quot;" class="wp-image-740" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_type_error.png 730w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/1_TypeScript_type_error-300x70.png 300w" sizes="(max-width: 730px) 100vw, 730px" /></figure></div>



<p>The compiler is right here. Our parameter is either <code>string</code> or <code>number</code>, so the <code>length</code> property may not be available if someone passes a <code>number</code>.</p>



<p>You might have already been in such situations. What did you do? Well, sometimes when the code is more complex, and you <em>just know</em> that the variable contains the requested property, it might be tempting to cast:</p>



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



<p>Of course, we will satisfy the compiler here. But does that make our code any more safe than the previous version? Not at all.</p>



<p>In our example, based on the code we have written, TypeScript is not able to determine the exact type of <code>variable</code>. We can help it by using <em>type guards</em>. We will talk about them in details in a separate article. However, for now it&#8217;s enough to know that the language has some built-in type guards &#8211; one of them is <a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#typeof-type-guards">typeof</a>. Let&#8217;s use it in our function:</p>



<script src="https://gist.github.com/dsibinski/3848759ec400644ce6b36326cb5b3e13.js"></script>



<p>That&#8217;s cool, TS compiler doesn&#8217;t complain anymore. But how does it work? Our code is still accessing <code>variable.length</code> directly, there&#8217;s no casting there.</p>



<p>Notice what your IDE tells you when you hover your mouse over the <code>variable</code> in the first <em>if</em> branch after checking if the type is <code>string</code>:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img decoding="async" width="559" height="202" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/2_TypeScript_narrowing1.png" alt="TypeScript types narrowing figures out the type of variable is &quot;string&quot;" class="wp-image-741" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/2_TypeScript_narrowing1.png 559w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/2_TypeScript_narrowing1-300x108.png 300w" sizes="(max-width: 559px) 100vw, 559px" /></figure></div>



<p>and also what it tells you when hovering over <code>variable</code> in the <code>else</code> block:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="560" height="178" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/3_TypeScript_narrowing2-1.png" alt="TypeScript types narrowing figures out the type of variable is &quot;number&quot;" class="wp-image-743" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/3_TypeScript_narrowing2-1.png 560w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/3_TypeScript_narrowing2-1-300x95.png 300w" sizes="auto, (max-width: 560px) 100vw, 560px" /></figure></div>



<p>Wow! The IDE magically knows what&#8217;s the exact type of our locally scoped variable! ?</p>



<p>This magic is called <em>types narrowing</em>. TypeScript compiler does many things to ensure that the type of local variable you work with is of the most specific type possible. It is based on the complex analysis of the source code, especially the code surrounding the place in which the local variable is used.</p>



<h2 class="wp-block-heading"><em>in</em> keyword in types narrowing</h2>



<p>Let&#8217;s take a look at this code now:</p>



<script src="https://gist.github.com/dsibinski/5ecdd052adb538e50e3f4917d11fe785.js"></script>



<p>Let&#8217;s say we have a variable which may either be <code>Employee</code> or <code>Manager</code>:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="166" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-1024x166.png" alt="TypeScript doesn't know the type is it's not narrowed" class="wp-image-756" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-1024x166.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-300x49.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1-768x125.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/5_TypeScript_narrowing_unknown_type-1.png 1189w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>In that place, TypeScript can&#8217;t say whether <code>person</code> is <code>Employee</code> or <code>Manager</code>.</p>



<p>Maybe you&#8217;ve used the <code>in</code> keyword for determining types before. Types narrowing also works with <code>in</code> keyword:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="688" height="228" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/6_TypeScript_narrowing_in_keyword.png" alt="in keyword helps TypeScript narrow the type" class="wp-image-757" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/6_TypeScript_narrowing_in_keyword.png 688w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/6_TypeScript_narrowing_in_keyword-300x99.png 300w" sizes="auto, (max-width: 688px) 100vw, 688px" /></figure></div>



<p>As you can see, TypeScript analyzed the flow, and it noticed that in the first <code>if</code> block we check whether the <code>person</code> object contains the <code>employees</code> property. If it does, the compiler known it must be <code>Manager</code>, not <code>Employee</code>. Cool, isn&#8217;t it? ?</p>



<h2 class="wp-block-heading">Narrowing and equality operator</h2>



<p>Another interesting application of TypeScript types narrowing is using the equality operator:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="780" height="417" src="https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality.png" alt="TypeScript types narrowing determines type of variable based on === equality operator" class="wp-image-760" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality.png 780w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality-300x160.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/10/7_TypeScript_narrowing_equality-768x411.png 768w" sizes="auto, (max-width: 780px) 100vw, 780px" /></figure></div>



<p>Notice how TypeScript determines the type of variables based on the <code>===</code> operator. If both parameters are equal, they must be both <code>string</code>s.</p>



<h2 class="wp-block-heading">TypeScript types narrowing &#8211; summary</h2>



<p>Narrowing in TypeScript is a very power feature of the language. I just showed you some simple examples, so you can grasp the idea and notice how much TS compiler does to ensure your type is <em>the correct one</em>.</p>



<p>If you&#8217;re interested, you can read much more about this in <a href="https://www.typescriptlang.org/docs/handbook/2/narrowing.html">TypeScript docs</a>. It&#8217;s however worth knowing how TypeScript compiler works and how you should properly check for types in your code. Remember that using <code>any</code> or explicit casting should be your last resort ?</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/">How Does TypeScript Types Narrowing Work?</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/10/how-does-typescript-types-narrowing-work/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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 loading="lazy" 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="auto, (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>
