<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dimitris Karagiannis</title>
    <description>The latest articles on DEV Community by Dimitris Karagiannis (@mitchkarajohn).</description>
    <link>https://dev.to/mitchkarajohn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F352418%2Fbd6e7e8f-adb0-44db-9707-66f425e5c932.jpg</url>
      <title>DEV Community: Dimitris Karagiannis</title>
      <link>https://dev.to/mitchkarajohn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mitchkarajohn"/>
    <language>en</language>
    <item>
      <title>You may not need axios</title>
      <dc:creator>Dimitris Karagiannis</dc:creator>
      <pubDate>Sat, 08 Jul 2023 12:09:27 +0000</pubDate>
      <link>https://dev.to/mitchkarajohn/you-may-not-need-axios-4h9o</link>
      <guid>https://dev.to/mitchkarajohn/you-may-not-need-axios-4h9o</guid>
      <description>&lt;p&gt;&lt;a href="https://axios-http.com/docs/intro"&gt;Axios&lt;/a&gt; is an awesome library; it offers you niceties and various built in utilities for handling HTTP requests. &lt;/p&gt;

&lt;p&gt;...but you may not need it. The native &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API"&gt;&lt;code&gt;fetch&lt;/code&gt;&lt;/a&gt; or &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest"&gt;&lt;code&gt;XMLHttpRequest&lt;/code&gt;&lt;/a&gt; APIs may be enough for your current needs. &lt;/p&gt;

&lt;p&gt;So, let's go over each of the featured, well, &lt;a href="https://axios-http.com/docs/intro"&gt;&lt;em&gt;features&lt;/em&gt;&lt;/a&gt; of axios and see how many of them are unique to axios.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;Before we start a little explanation on the verdicts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;You do not need axios for this 👎&lt;/strong&gt;: Means that this feature is something that is already ready to use out of the box with the native APIs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You may not need axios for this 😐&lt;/strong&gt;: Means that this feature is something that you can fairly easily implement it yourself with &lt;code&gt;fetch&lt;/code&gt; or &lt;code&gt;XMLHttpRequest&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You may need axios for this 👍&lt;/strong&gt;: Means that this feature is something that is not very easy to implement it yourself and as such you may want to reach to axios for it&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Make XMLHttpRequests from the browser
&lt;/h3&gt;

&lt;p&gt;Good news, you can do that with both &lt;code&gt;fetch&lt;/code&gt; and &lt;code&gt;XMLHttpRequests&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You do not need axios for this 👎.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make http requests from node.js
&lt;/h3&gt;

&lt;p&gt;Node's "equivalent" for &lt;code&gt;XMLHttpRequest&lt;/code&gt; is &lt;a href="https://nodejs.org/api/http.html"&gt;&lt;code&gt;http&lt;/code&gt;&lt;/a&gt;. Most recent versions of Node have also added support for &lt;code&gt;fetch&lt;/code&gt; as well. But &lt;strong&gt;if&lt;/strong&gt; you have to write server code &lt;strong&gt;and if&lt;/strong&gt; you want to write it in Node &lt;strong&gt;and if&lt;/strong&gt; you need some level of parity between the front and back end codebases &lt;strong&gt;then&lt;/strong&gt; having to only learn and use one API (that of axios) may be useful. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may not need axios for this 😐.&lt;/p&gt;

&lt;h3&gt;
  
  
  Supports the Promise API
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;fetch&lt;/code&gt; is Promise based. It is also pretty trivial to make a Promise wrapper for &lt;code&gt;XMLHttpRequest&lt;/code&gt; if you so wish.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You do not need axios for this 👎.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intercept request and response
&lt;/h3&gt;

&lt;p&gt;If you know you will have to do this and you do not want to build this functionality yourself then having a built in way to do it is surely useful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may need axios for this 👍.&lt;/p&gt;

&lt;h3&gt;
  
  
  Transform request and response data
&lt;/h3&gt;

&lt;p&gt;Same as above, if you know you will need this and you do not want to built it yourself axios is a good choice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may need axios for this 👍.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cancel requests
&lt;/h3&gt;

&lt;p&gt;You can do this with both &lt;code&gt;fetch&lt;/code&gt; and &lt;code&gt;XMLHttpRequest&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You do not need axios for this 👎.&lt;/p&gt;

&lt;h3&gt;
  
  
  Timeouts
&lt;/h3&gt;

&lt;p&gt;Keeping in mind you can cancel requests with both &lt;code&gt;fetch&lt;/code&gt; and &lt;code&gt;XMLHttpRequest&lt;/code&gt; it is not so difficult to "timeout" a request using &lt;code&gt;setTimeout&lt;/code&gt;. But, as always, &lt;strong&gt;if&lt;/strong&gt; you know you will be requiring this functionality a lot &lt;strong&gt;and if&lt;/strong&gt; you do not want to spent any time at all to implement it yourself &lt;strong&gt;then&lt;/strong&gt; you can use axios I guess. But come on, it's just a &lt;code&gt;setTimeout&lt;/code&gt; 😅&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may not need axios for this 😐.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query parameters serialization with support for nested entries
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams"&gt;&lt;code&gt;URLSearchParams&lt;/code&gt;&lt;/a&gt; exists. Yes, if you are using TypeScript it may be a little stingy because it actually requires that you pass a &lt;code&gt;Record&amp;lt;string, string&amp;gt;&lt;/code&gt; object (even though passing something like &lt;code&gt;Record&amp;lt;string, number&amp;gt;&lt;/code&gt; will also work in reality) but overall it's easy to use and it's a native way to manage your query params.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: If you use TypeScript and you are ok with having to pass only &lt;code&gt;Record&amp;lt;string, string&amp;gt;&lt;/code&gt; objects to &lt;code&gt;URLSearchParams&lt;/code&gt; you do not need axios for this 👎.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic request body serialization
&lt;/h3&gt;

&lt;p&gt;While not "automatic", all you need to do when you send a request is to match the data you are sending with the appropriate &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type"&gt;&lt;code&gt;Content-Type&lt;/code&gt;&lt;/a&gt; header. Axios can automatically transform plain JS objects to &lt;code&gt;FormData&lt;/code&gt; objects if you define the request &lt;code&gt;Content-Type&lt;/code&gt; header as &lt;code&gt;multipart/form-data&lt;/code&gt; which is nice, but why not just try to send what your API expects?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may not need axios for this 😐.&lt;/p&gt;

&lt;h3&gt;
  
  
  Posting HTML forms as JSON
&lt;/h3&gt;

&lt;p&gt;This had me quite confused. Posting a form as JSON is not a task that befalls on axios, rather the user, who has to collect the form data and convert them to a suitable format for sending them as &lt;code&gt;application/json&lt;/code&gt; instead of &lt;code&gt;multipart/form-data&lt;/code&gt;. Really not sure what they mean here, but from what I understand 👇&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You do not need axios for this 👎.&lt;/p&gt;

&lt;h3&gt;
  
  
  Automatic JSON data handling in response
&lt;/h3&gt;

&lt;p&gt;Not sure what is automatically handled in the response's JSON data here. If someone knows feel free to chime in the comments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: ??&lt;/p&gt;

&lt;h3&gt;
  
  
  Progress capturing for browsers and node.js with extra info (speed rate, remaining time)
&lt;/h3&gt;

&lt;p&gt;This is hands down one of the best features of axios. While this not very difficult to implement it yourself using &lt;code&gt;XMLHttpRequest&lt;/code&gt;, you will not be so lucky if you have chosen to go with &lt;code&gt;fetch&lt;/code&gt; and do not want to be mixing them up. If you need this functionality the fact that it's ready to use here out of the box is a godsend; we are not talking about a &lt;code&gt;setTimeout&lt;/code&gt; implementation here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may need axios for this 👍.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting bandwidth limits for node.js
&lt;/h3&gt;

&lt;p&gt;This is another one of those things where you probably do not want to implement yourself from scratch, because it is not a trivial implementation. If you need this functionality you will probably appreciate the fact that someone already implemented this for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may need axios for this 👍.&lt;/p&gt;

&lt;h3&gt;
  
  
  Client side support for protecting against XSRF
&lt;/h3&gt;

&lt;p&gt;All this does is read a &lt;a href="https://en.wikipedia.org/wiki/Cross-site_request_forgery"&gt;&lt;code&gt;CSRF&lt;/code&gt;&lt;/a&gt; related cookie from your environment and pass it along with the request headers (&lt;a href="https://github.com/axios/axios/issues/2327"&gt;ref&lt;/a&gt;). Not something you cannot do yourself easily, but automating this is nice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verdict&lt;/strong&gt;: You may not need axios for this 😐.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overall
&lt;/h2&gt;

&lt;p&gt;I guess the point I want to make with this post is to &lt;em&gt;always weight your needs and your options and decide based on them&lt;/em&gt;. If your needs change then reconsider your options and change them if needed.&lt;/p&gt;

&lt;p&gt;Do not use a tool blindly just because you saw it in a StackOverflow answer or a tutorial or simply because it's the flavour of the month.&lt;/p&gt;

&lt;p&gt;This is not something specific to axios. I like axios, I have used it a lot during my career, I do not have any problem with it; it's a fine tool that does what it does very well. Axios is just a nice case to analyse in this way.&lt;/p&gt;

&lt;p&gt;Thank you for reading 🎉&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>javascriptlibraries</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Working with React context providers in Typescript</title>
      <dc:creator>Dimitris Karagiannis</dc:creator>
      <pubDate>Thu, 19 Mar 2020 13:48:06 +0000</pubDate>
      <link>https://dev.to/mitchkarajohn/working-with-react-context-providers-in-typescript-1fk0</link>
      <guid>https://dev.to/mitchkarajohn/working-with-react-context-providers-in-typescript-1fk0</guid>
      <description>&lt;p&gt;&lt;strong&gt;Disclaimer 📣&lt;br&gt;
This post was originally a part of my &lt;a href="https://dev.to/mitchkarajohn/tips-for-migrating-a-react-app-to-typescript-4jpk"&gt;other&lt;/a&gt; article, but, since it became quite big, I decided to break it into its own mini post.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Say we have a simple provider that takes an &lt;a href="https://github.com/axios/axios"&gt;&lt;code&gt;axios&lt;/code&gt;&lt;/a&gt; instance as a prop and provides it to the rest of the application via context&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AxiosContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;AxiosProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;axiosInstance&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AxiosContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;axiosInstance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;AxiosContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useAxios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AxiosContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AxiosProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useAxios&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, let's write this in TS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AxiosInstance&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ReactNode&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AxiosContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReactNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;axiosInstance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AxiosInstance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;AxiosProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;axiosInstance&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AxiosContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;axiosInstance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;AxiosContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;useAxios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AxiosContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AxiosProvider&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useAxios&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All is well now, right? We defined the &lt;code&gt;Props&lt;/code&gt; type, so we are good to go. Well, not exactly. This will not work right away, because when we did&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AxiosContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we implicitly set the type of the provider value to &lt;code&gt;undefined&lt;/code&gt; and thus doing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;AxiosContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Provider&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;axiosInstance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will throw a TS error, since the value we are passing is of &lt;code&gt;AxiosInstance&lt;/code&gt; type, according to our &lt;code&gt;Props&lt;/code&gt; type declaration, but is also &lt;code&gt;undefined&lt;/code&gt; according to the context initialisation.&lt;/p&gt;

&lt;p&gt;To fix this we declare a new type like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ContextValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;AxiosInstance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which can be further broken into&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProviderValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AxiosInstance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// since you know this is what the provider will be passing&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;DefaultValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ContextValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DefaultValue&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ProviderValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then declare the type during the context initialisation like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AxiosContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ContextValue&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we let TS know that the context value can either be &lt;code&gt;undefined&lt;/code&gt; (the default value) or an &lt;code&gt;AxiosInstance&lt;/code&gt; (which is what will actually be returned by your provider). Now everything is ok then? Not yet, hang in there. &lt;/p&gt;

&lt;p&gt;Because, now if we use the &lt;code&gt;useAxios&lt;/code&gt; hook inside another component and try to use the value it returns, we will get a TS error telling us that the return value of &lt;code&gt;useAxios&lt;/code&gt; can be &lt;code&gt;undefined&lt;/code&gt; since this is how we defined it when we initialised the &lt;code&gt;AxiosContext&lt;/code&gt;. How do we tackle this problem? We'll take a two-pronged approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  A development time solution
&lt;/h3&gt;

&lt;p&gt;As the programmer, we know that when we use the &lt;code&gt;useAxios&lt;/code&gt; hook, the value it will return will never be &lt;code&gt;undefined&lt;/code&gt;. It will always be of type &lt;code&gt;ProviderValue&lt;/code&gt; since we know that we are using the hook inside a component that is a child of the &lt;code&gt;AxiosProvider&lt;/code&gt; (because this is how we must use context hooks in order for them to work). &lt;/p&gt;

&lt;p&gt;So, the fix here is simple, and it's a type assertion. When we use the &lt;code&gt;useAxios&lt;/code&gt; hook, we should always assert that its type is of &lt;code&gt;ProviderValue&lt;/code&gt; like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useAxios&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ProviderValue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;Path_to_AxiosProvider&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;SomeComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;axiosInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useAxios&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;ProviderValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// Do something with the axiosInstance object&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and TS now knows that this is in fact an &lt;code&gt;axios&lt;/code&gt; instance object.&lt;/p&gt;

&lt;h3&gt;
  
  
  A runtime approach
&lt;/h3&gt;

&lt;p&gt;The above solution just solves the issue during development. But what happens if a new developer comes along, who they don't know that in order to use a React context value the component using it must be a child of the  Provider component? This is a case where the assertion we made above stops being true during runtime and the whole app crashes because we try to access stuff on an &lt;code&gt;axiosInstance&lt;/code&gt; that is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We could add a&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;axiosInstance&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;The component using the the context must be a descendant of the context provider&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;right after we do &lt;code&gt;const axiosInstance = useAxios()&lt;/code&gt; but in that case the type assertion we did earlier is useless and we also need to be writing this runtime check every time we make use of &lt;code&gt;useAxios&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The solution I've come up with for this is the following:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy"&gt;Proxy&lt;/a&gt; as the default context value&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Proxies are very useful in that they allow you to completely define the behaviour of a proxied object.&lt;/p&gt;

&lt;p&gt;To elaborate, remember how we initialise our context, currently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AxiosContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ContextValue&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, what if instead of &lt;code&gt;undefined&lt;/code&gt; we initialised the context with a &lt;code&gt;Proxy&lt;/code&gt; of a random &lt;code&gt;axios&lt;/code&gt; instance object? like so&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AxiosContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ContextValue&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Proxy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our types definition can now also change to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ProviderValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AxiosInstance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;DefaultValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;AxiosInstance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ContextValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DefaultValue&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ProviderValue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is still not enough. We want the app to throw in case the default context is used, with an appropriate error message (and we do not want to do this check every time we use the &lt;code&gt;useAxios&lt;/code&gt; hook, because we are lazy)&lt;/p&gt;

&lt;p&gt;So, we simply define what we want to happen if the application code tries to access any members of this proxied &lt;code&gt;axios&lt;/code&gt; instance that we return as a default context value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;AxiosContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ContextValue&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Proxy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You must wrap your component in an AxiosProvider&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;You must wrap your component in an AxiosProvider&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;apply&lt;/code&gt; handles the behaviour when we try to call any methods from the proxied &lt;code&gt;axios&lt;/code&gt; instance object and &lt;code&gt;get&lt;/code&gt; handles the behaviour when we try to access any of its properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  In conclusion
&lt;/h2&gt;

&lt;p&gt;With the above approach we both keep Typescript satisfied and we also need to write the least code possible: Just a type assertion when we use the context hook and define the default context value as a proxy which throws if any code tries to access it.&lt;/p&gt;

&lt;p&gt;Thanks for reading! 🎉&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Tips for migrating a React app to Typescript</title>
      <dc:creator>Dimitris Karagiannis</dc:creator>
      <pubDate>Thu, 19 Mar 2020 13:47:17 +0000</pubDate>
      <link>https://dev.to/mitchkarajohn/tips-for-migrating-a-react-app-to-typescript-4jpk</link>
      <guid>https://dev.to/mitchkarajohn/tips-for-migrating-a-react-app-to-typescript-4jpk</guid>
      <description>&lt;p&gt;So, I've been working on a company project for almost half a year now. After feature development was finished and work on it slowed down, I decided I wanted to take the next step, which I did not dare take 6 months back, and write (or in this case "migrate") the project to Typescript. &lt;/p&gt;

&lt;p&gt;These are all the things I learned in the process and, in retrospect, I wish I knew when I was starting the migration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer 📣
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;This post assumes you are a beginner with TS but its purpose is not to teach you TS. Its purpose is to give some advice to the next person who wants to try something similar and make their lives easier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This post assumes that you are working on a CRA project and most of the setup and configuration is already been taken care for you. However most of the advice could be applied on any React project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This post is based on &lt;em&gt;my&lt;/em&gt; experience with migrating a moderately big codebase to TS. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Before you begin ⚙️
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Set &lt;code&gt;allowJs&lt;/code&gt; in your tsconfig to &lt;code&gt;true&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;That's all. This will ensure that you are not drowning in red the moment you enable the type checker and will allow you to migrate gradually; letting you keep your existing &lt;code&gt;.js&lt;/code&gt; files, until their time comes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create a &lt;code&gt;global.d.ts&lt;/code&gt; file in your &lt;code&gt;src&lt;/code&gt; directory
&lt;/h3&gt;

&lt;p&gt;This will come in handy, since you most likely will want to &lt;a href="https://www.typescriptlang.org/docs/handbook/declaration-merging.html"&gt;augment or extend&lt;/a&gt; the types of existing libraries you are using. &lt;/p&gt;

&lt;h3&gt;
  
  
  Create a &lt;code&gt;custom.d.ts&lt;/code&gt; file in your &lt;code&gt;src&lt;/code&gt; directory
&lt;/h3&gt;

&lt;p&gt;You will need this, in order to be able to &lt;a href="https://webpack.js.org/guides/typescript/#importing-other-assets"&gt;import files that are not code modules&lt;/a&gt;, such as image files, &lt;code&gt;.json&lt;/code&gt; files, etc. To help you get started, just add this inside your &lt;code&gt;custom.d.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*.svg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ReactComponent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;FunctionComponent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;SVGProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SVGSVGElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kr"&gt;module&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Et voila, now you can import &lt;code&gt;.svg&lt;/code&gt; and &lt;code&gt;.json&lt;/code&gt; files inside your code modules without TS throwing errors at you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should you want to enable &lt;code&gt;noImplicitAny&lt;/code&gt; and &lt;code&gt;no-explicit-any&lt;/code&gt; do it before you start
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;noImplicitAny&lt;/code&gt; is a &lt;a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html"&gt;compiler option&lt;/a&gt; which will raise errors on expressions and declarations with an implied &lt;code&gt;any&lt;/code&gt; type.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;no-explicit-any&lt;/code&gt; is an &lt;a href="https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-explicit-any.md"&gt;&lt;code&gt;eslint&lt;/code&gt; rule&lt;/a&gt; that doesn't allow you to define anything as &lt;code&gt;any&lt;/code&gt; type. &lt;/p&gt;

&lt;p&gt;If you do enable these options, the &lt;a href="https://www.google.com/search?q=typescript+unknown+vs+any&amp;amp;oq=typescript+unknown"&gt;&lt;code&gt;unknown&lt;/code&gt;&lt;/a&gt; type will be your friend. &lt;/p&gt;

&lt;p&gt;These options should ideally be enabled from the get go. No matter what, &lt;strong&gt;do not enable them during the migration process&lt;/strong&gt;. I made that mistake and ended up suddenly having to spend lots and lots of time resolving quite an amount of errors that I "inflicted" upon myself. Can be quite disheartening while you are still in the middle of the migration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup your &lt;code&gt;eslint&lt;/code&gt; config for use with TS
&lt;/h3&gt;

&lt;p&gt;I happened to have an issue with &lt;code&gt;eslint&lt;/code&gt;'s default &lt;code&gt;no-unused-vars&lt;/code&gt; rule and TS. Turns out there is a specific &lt;a href="https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md"&gt;&lt;code&gt;no-unused-vars&lt;/code&gt;&lt;/a&gt; rule for TS. Enable this and &lt;a href="https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unused-vars.md#options"&gt;disable the default one&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Settle on how you will be defining your types
&lt;/h3&gt;

&lt;p&gt;Will you be using interfaces or types? Will you be writing your types inside the code file or as a separate file? I advise that you settle on these details before you start because you don't want to change you mind halfway there and have to retrofix all the files you have already worked on. &lt;/p&gt;

&lt;p&gt;My advice is use types unless an interface is absolutely necessary and keep your type definitions separate from the component files themselves. &lt;/p&gt;

&lt;p&gt;The system I applied was the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the file is a React component file, create a separate &lt;code&gt;types.d.ts&lt;/code&gt; file in the same directory and put all the type definitions there. Except for the &lt;code&gt;Props&lt;/code&gt; type definition; I keep that in the component file itself, since it is handy to have the props definition readily available.&lt;/li&gt;
&lt;li&gt;If the file is not a React component type, declarations go along with the code. No separate &lt;code&gt;types.d.ts&lt;/code&gt; file. That is unless the types are so many that make the code file really messy, then they are taken out in their own file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Read the documentation
&lt;/h3&gt;

&lt;p&gt;Seriously. At least read some of the important parts, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/basic-types.html"&gt;Basic types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/generics.html"&gt;Generics&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html"&gt;Advanced types&lt;/a&gt; - Especially how &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#intersection-types"&gt;intersections&lt;/a&gt; and &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#union-types"&gt;unions&lt;/a&gt; work, because they do not map exactly 1:1 with the mathematical definition of unions and intersections&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/type-compatibility.html"&gt;Type compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.typescriptlang.org/docs/handbook/utility-types.html"&gt;Utility types&lt;/a&gt; - Those are &lt;em&gt;very&lt;/em&gt; handy, give them a look to, at least, know of their existence.&lt;/li&gt;
&lt;li&gt;Release notes - I have found that sometimes things mentioned in the release notes of a new version are not mentioned in the docs. For example this &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions"&gt;very handy assertion function functionality&lt;/a&gt; which was added in version 3.7 and AFAIK is not mentioned anywhere in the docs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Prepare to start being more thorough when you code
&lt;/h3&gt;

&lt;p&gt;Typescript will eventually lead you to be more thorough with some assumptions you make while coding. Assumptions for which, in your mind, know to be correct for your code, are not enough for TS. Typescript will always be asking to validate those assumptions by writing some more code, mostly in the form of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notCondition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Otherwise all is well&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll get used to it, and eventually it will come to you naturally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get ready to be wrong
&lt;/h3&gt;

&lt;p&gt;Accept the fact that 99.999% of times the TS compiler will be right and you will be wrong 😅&lt;/p&gt;

&lt;h2&gt;
  
  
  On with the actual migration 🚀
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Start small
&lt;/h3&gt;

&lt;p&gt;When you make the transition from JS to TS you want to start small. See that directory with your utility functions? They are by far the simplest things you can start migrating over to TS. &lt;/p&gt;

&lt;p&gt;Once you migrate a simple function see where this function is used, what other files import it (the compiler will probably let you know, by throwing some errors at you at this point). &lt;/p&gt;

&lt;p&gt;Proceed with migrating those files, and repeat the process, essentially climbing up (down?) the dependency tree. If you reach a file that is far too complex for this stage of the migration don't be afraid to put a &lt;code&gt;@ts-nocheck&lt;/code&gt; directive at the first line of it, and migrate it at a later time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do not be afraid to do type assertions
&lt;/h3&gt;

&lt;p&gt;Type assertions in TS are like type casting in other languages. In essence you are telling the TS compiler that you know better, and a specific variable, even though it &lt;em&gt;could&lt;/em&gt; possibly be of many types, cannot be anything else other than a specific type in this case. Sometimes you &lt;em&gt;do&lt;/em&gt; in fact know better. But only sometimes 😅&lt;/p&gt;

&lt;p&gt;I have found this to be useful when working with React hooks that provide some &lt;a href="https://reactjs.org/docs/hooks-reference.html#usecontext"&gt;context&lt;/a&gt; value. Context values start with a "default" value when you initialise the &lt;code&gt;Context&lt;/code&gt;, which may not always be compatible with the actual value passed to the provider.  More on that in &lt;a href="https://dev.to/mitchkarajohn/working-with-react-context-providers-in-typescript-1fk0"&gt;this&lt;/a&gt; post.&lt;/p&gt;

&lt;p&gt;Custom &lt;a href="https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types"&gt;type guards&lt;/a&gt; and &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions"&gt;assertion functions&lt;/a&gt; are also very useful in helping you help TS make the correct type assertions&lt;/p&gt;

&lt;h3&gt;
  
  
  Do not use the provided &lt;code&gt;FC&lt;/code&gt; type for React components
&lt;/h3&gt;

&lt;p&gt;I highly encourage you to not define React components like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Comp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;....&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I found its usage to be more of a bother than anything, since it makes some rather arbitrary assumptions about your components (like always having &lt;code&gt;children&lt;/code&gt; props and not working very well with &lt;code&gt;PropTypes&lt;/code&gt;) and in general takes away some of the control you have over defining your components the way you want. Use&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Comp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;....&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead. TS is smart enough to infer the return type itself, and for all intents and purposes this is a valid React component, which you can use in any case where a React component is expected by the compiler. &lt;/p&gt;

&lt;h3&gt;
  
  
  Keep your &lt;code&gt;PropTypes&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;While TS is very useful in making sure you are not making any type related mistakes during development time, &lt;code&gt;PropTypes&lt;/code&gt; are very useful in letting you know of type related mistakes during runtime. How can you have type related errors during runtime, if you are using TS for development, you ask? Consider this scenario: &lt;/p&gt;

&lt;p&gt;You have defined your API response type in TS as bringing back a field which is supposed to be a &lt;code&gt;number&lt;/code&gt;. You have also defined your relevant &lt;code&gt;PropTypes&lt;/code&gt; field as such. All is well. &lt;/p&gt;

&lt;p&gt;Now, imagine if your API returns a &lt;code&gt;string&lt;/code&gt; instead of a &lt;code&gt;number&lt;/code&gt; in that field. Had you removed the &lt;code&gt;PropTypes&lt;/code&gt; you would never realise the error early, until the app crashed at some point. With &lt;code&gt;PropTypes&lt;/code&gt; you will get a very useful warning in the browser console if any such mismatch ever occurs.&lt;/p&gt;

&lt;p&gt;If you work on an ejected CRA project, or otherwise you have access to the &lt;code&gt;babelrc&lt;/code&gt; file, know that a &lt;a href="https://www.npmjs.com/package/babel-plugin-typescript-to-proptypes"&gt;plugin&lt;/a&gt; exists that can automatically convert your TS types to &lt;code&gt;PropTypes&lt;/code&gt;, so that you don't have to manually update both.&lt;/p&gt;

&lt;h3&gt;
  
  
  Export all your types
&lt;/h3&gt;

&lt;p&gt;Even if you don't end up importing all of them in other files, make a habit of exporting them since you never know when you may need a type which is defined in another module.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do not be afraid to use generics
&lt;/h3&gt;

&lt;p&gt;TS &lt;a href="https://www.typescriptlang.org/docs/handbook/generics.html"&gt;generics&lt;/a&gt; can be really helpful and you can have generic React components as well. For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Comp.tsx&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Comp&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Props&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// T can also be used inside the function body too, if needed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// OtherComp.tsx&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;SomeType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;OtherComp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Comp&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SomeType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;someProp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{...}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SomeContent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Comp&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my experience, if a React component is an abstraction over some thing, then it's safe to assume that this component's type definition will be a generic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Read the type definitions of libraries you use
&lt;/h3&gt;

&lt;p&gt;When in doubt always read the type definitions for the libraries you use. This will also help you on how you should define your own component types for usage with components from external libraries.&lt;/p&gt;

&lt;h3&gt;
  
  
  You don't always have to define the return types
&lt;/h3&gt;

&lt;p&gt;TS is (mostly) smart enough that it will correctly infer the type of the return values of functions. Personally, I like to define return values too, but that's because I have some kind of OCD 😅&lt;/p&gt;

&lt;p&gt;Do note that there may be times that you'll have to strictly define the return value for things to work correctly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make type definitions for your API responses
&lt;/h3&gt;

&lt;p&gt;They will help you immensely, since it's more than likely that you will be using server provided data in parts of your application&lt;/p&gt;

&lt;h3&gt;
  
  
  Learn to read TS compiler errors
&lt;/h3&gt;

&lt;p&gt;TS errors can be intimidating, however there is a "cheat" in how to read them. Always read the first and the last few lines of the error message. If you still don't make sense do read the whole error, but usually just reading those lines will give you the information you need to make sense of the error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Random Typescript tip ✅
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Do &lt;code&gt;(TypeA | TypeB)[]&lt;/code&gt; don't &lt;code&gt;TypeA[] | TypeB[]&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When you have an array whose type may be either an array of &lt;code&gt;TypeA&lt;/code&gt; or an array of &lt;code&gt;TypeB&lt;/code&gt; declare it as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;TypeA&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;TypeB&lt;/span&gt;&lt;span class="p"&gt;)[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TypeA&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;TypeB&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a &lt;a href="https://stackoverflow.com/a/49511416/4651083"&gt;subtle difference&lt;/a&gt; between those 2 declarations and the second one will lead to errors if you try to &lt;code&gt;.map()&lt;/code&gt; over the &lt;code&gt;arr&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing words
&lt;/h2&gt;

&lt;p&gt;The fact that you decided to &lt;em&gt;migrate&lt;/em&gt; your app instead of writing it in TS from the start, may conceal the merits of having used TS at the beginning, since your whole code base is already laid out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;However&lt;/strong&gt; you &lt;em&gt;will&lt;/em&gt; see the merits once you continue development and now all your new code will have to abide by the TS rules.&lt;/p&gt;

&lt;p&gt;Remember, your code is now quite a lot more thorough with handling error cases. &lt;/p&gt;

&lt;p&gt;This, combined with some proper tests, will ensure that your code is as robust as it can be.&lt;/p&gt;

&lt;p&gt;Thank you for reading! 🎉&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
