<?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>Forem: Daniel Hauser</title>
    <description>The latest articles on Forem by Daniel Hauser (@danielhauser).</description>
    <link>https://forem.com/danielhauser</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%2F272477%2F6733ecd1-9a48-4108-99e7-530af83ab05b.jpeg</url>
      <title>Forem: Daniel Hauser</title>
      <link>https://forem.com/danielhauser</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/danielhauser"/>
    <language>en</language>
    <item>
      <title>Convert a react app from flow to typescript without losing git history</title>
      <dc:creator>Daniel Hauser</dc:creator>
      <pubDate>Wed, 20 Nov 2019 18:35:54 +0000</pubDate>
      <link>https://forem.com/danielhauser/convert-a-react-app-from-flow-to-typescript-without-losing-git-history-32i1</link>
      <guid>https://forem.com/danielhauser/convert-a-react-app-from-flow-to-typescript-without-losing-git-history-32i1</guid>
      <description>&lt;h2&gt;
  
  
  start with creating a new branch
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; &lt;span class="s2"&gt;"moving-from-flow-to-ts"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Rename files from &lt;code&gt;js&lt;/code&gt; to &lt;code&gt;ts[x]&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Lets start with renaming all &lt;code&gt;.js&lt;/code&gt; files to &lt;code&gt;.ts&lt;/code&gt; with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; ./src/&lt;span class="k"&gt;**&lt;/span&gt;/&lt;span class="k"&gt;*&lt;/span&gt;.js | 
    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;read &lt;/span&gt;line&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do &lt;/span&gt;git &lt;span class="nb"&gt;mv&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;$line&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;line&lt;/span&gt;&lt;span class="p"&gt;%.js&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;.ts&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, assuming all files importing react are jsx files, rename them from &lt;code&gt;.ts&lt;/code&gt; to &lt;code&gt;.tsx&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;find ./src &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-name&lt;/span&gt; &lt;span class="s2"&gt;"*.ts"&lt;/span&gt; |
    xargs &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s1"&gt;'import React[ ,]'&lt;/span&gt; |
    &lt;span class="nb"&gt;cut&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt;: &lt;span class="nt"&gt;-f1&lt;/span&gt; |
    &lt;span class="nb"&gt;uniq&lt;/span&gt; | 
    &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="nb"&gt;read &lt;/span&gt;line&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do &lt;/span&gt;git &lt;span class="nb"&gt;mv&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nv"&gt;$line&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;line&lt;/span&gt;&lt;span class="p"&gt;%.ts&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;.tsx&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Commit to save the new file names&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"rename js files to ts and tsx"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Move to typescript
&lt;/h2&gt;

&lt;p&gt;Remove flow-bin from package.json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm uninstall flow-bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, install and initialize typescript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; typescript @types/react @types/react-dom &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; tsc &lt;span class="nt"&gt;--init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you use decorators, enable the &lt;code&gt;experimentalDecorators&lt;/code&gt; flag in &lt;code&gt;tsconfig.json&lt;/code&gt; before running the next step&lt;/p&gt;

&lt;p&gt;Commit to save the changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"replace flow with typescript"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Convert all flow types to typescript types
&lt;/h2&gt;

&lt;p&gt;Fix types in all tsx files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npx flow-to-ts &lt;span class="nt"&gt;--inline-utility-types&lt;/span&gt; &lt;span class="nt"&gt;--write&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; tsx ./src/&lt;span class="k"&gt;**&lt;/span&gt;/&lt;span class="k"&gt;*&lt;/span&gt;.tsx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Fix types in all ts files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npx flow-to-ts &lt;span class="nt"&gt;--inline-utility-types&lt;/span&gt; &lt;span class="nt"&gt;--write&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; ts ./src/&lt;span class="k"&gt;**&lt;/span&gt;/&lt;span class="k"&gt;*&lt;/span&gt;.ts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Next step
&lt;/h2&gt;

&lt;p&gt;Run &lt;code&gt;tsc --noEmit&lt;/code&gt; to see compilation errors, and fix them manually&lt;/p&gt;

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