How to Migrate your React Project to TypeScript

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • react-app

    This is in our handleChange function that’s attached to our element. When our user types in the </code>, the event is passed to the <code>handleChange</code> function and becomes the value for our <code>setAffirmation</code> hook. We know that we don’t want the type of our event to be <code>any</code>. This is a journal app and that value is text, or more specifically, a string.</p> <p>We need to let the application know that we don’t want it to accept any other type. We’re going to give our function a type of <code>React.ChangeEventHandler<HTMLTextAreaElement></code> since we have access to <code>React.ChangeEventHandler</code> through <code>@types/react</code>. This is what it will look like if you use inline types:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="kd">const</span> <span class="nx">handleChange</span><span class="p">:</span> <span class="nx">React</span><span class="p">.</span><span class="nx">ChangeEventHandler</span><span class="o"><</span><span class="nx">HTMLTextAreaElement</span><span class="o">></span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">setAffirmation</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span><span class="p">)</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The next update we need to make is in our <code>handleSubmit</code> function:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="kd">const</span> <span class="nx">handleSubmit</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">()</span> <span class="nx">setFinalAffirmation</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>If we hover over the <code>e</code>, we get the same message we did above. You might be tempted to type it in the same way, but let’s think about what’s happening here for a second. We’re submitting a completed form here. If we head over to the <a href="https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/forms_and_events/#list-of-event-types">React TypeScript Cheatsheet</a>, we’ll find an event type of <code>FormEvent</code>, which they describe as an “Event that occurs whenever a form or form element gets/loses focus, a form element value is changed or the form is submitted.” That sounds like a good choice.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="cm">/* submit is a form event */</span> <span class="kd">const</span> <span class="nx">handleSubmit</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">:</span> <span class="nx">React</span><span class="p">.</span><span class="nx">FormEvent</span><span class="o"><</span><span class="nx">HTMLFormElement</span><span class="o">></span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="cm">/* prevent page from refreshing */</span> <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">()</span> <span class="nx">setFinalAffirmation</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>We add a comment there for clarity that submit is a form event, and then we specify the type as an <code>HTMLFormElement</code>.</p> <p>We’ll move on to the last one in this example.</p> <p>Around line 57, we can see that TypeScript is complaining about <code>socketRef.current</code>. If we hover over it, we get this message:<br> <a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MZeL3Owj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uu1y0hn4dvb0xo76tlim.png" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MZeL3Owj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uu1y0hn4dvb0xo76tlim.png" alt="Type 'WebSocket' is not assignable to type 'null'" loading="lazy" width="880" height="151"></a></p> <p>Because we added the WebSocket types package, <code>@types/websocket</code>, we have access to a WebSocket type. However, we don’t set the type on line 57. The actual problem is with <code>const socketRef = useRef(null)</code>.</p> <p>We need to let TypeScript know that we’re looking at a WebSocket or null: <code>const socketRef = useRef<WebSocket | null>(null)</code>.</p> <p>With that last update, we no longer have TypeScript complaining in this file. That doesn’t mean that there aren’t other updates that we can make. If you hover over other variables or functions, we can see what TypeScript is inferring.</p> <p><a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4MI3Ycnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny009v1ukv7hxfz820uo.gif" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4MI3Ycnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny009v1ukv7hxfz820uo.gif" alt="gif hovering over code" loading="lazy" width="880" height="357" data-animated="true"></a></p> <p>To complete this migration, we need to update the .js files to .tsx. But the nice thing is that we don’t have to do that all at once. For now, this is where we’ll leave it.</p> <h2> <a name="learn-more-about-typescript" href="#learn-more-about-typescript"> </a> Learn More About TypeScript </h2> <p>There’s a lot to learn about TypeScript. Here are some resources I've found helpful:</p> <ul> <li><a href="https://www.typescriptlang.org/docs/handbook/intro.html">TypeScript Handbook</a></li> <li><a href="https://www.totaltypescript.com/tutorials/beginners-typescript">Totally TypeScript</a></li> <li><a href="https://github.com/typescript-cheatsheets/react#reacttypescript-cheatsheets">React + TypeScript Cheatsheets</a></li> </ul> <p>And check out the Bonus Section of Sandra’s post on how to <a href="https://blog.deepgram.com/build-a-todo-list-with-pinia-and-vue-3/#bonus-section:-add-typescript">Build a To-do List App with Pinia and Vue 3</a>.</p> <p>To access the code for this blog post, select the <code>feature/typescript</code> branch from the <a href="https://github.com/deepgram-devs/react-app">React App GitHub Repository</a>. If you have any thoughts, comments, or questions, please drop a comment in our <a href="https://github.com/orgs/deepgram/discussions/categories/general">General Discussion category</a>, or if you want to say hey, drop your intro in our <a href="https://github.com/orgs/deepgram/discussions/3">intro thread</a>.</p>

  • react

    Cheatsheets for experienced React developers getting started with TypeScript (by typescript-cheatsheets)

    This is in our handleChange function that’s attached to our element. When our user types in the </code>, the event is passed to the <code>handleChange</code> function and becomes the value for our <code>setAffirmation</code> hook. We know that we don’t want the type of our event to be <code>any</code>. This is a journal app and that value is text, or more specifically, a string.</p> <p>We need to let the application know that we don’t want it to accept any other type. We’re going to give our function a type of <code>React.ChangeEventHandler<HTMLTextAreaElement></code> since we have access to <code>React.ChangeEventHandler</code> through <code>@types/react</code>. This is what it will look like if you use inline types:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="kd">const</span> <span class="nx">handleChange</span><span class="p">:</span> <span class="nx">React</span><span class="p">.</span><span class="nx">ChangeEventHandler</span><span class="o"><</span><span class="nx">HTMLTextAreaElement</span><span class="o">></span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">setAffirmation</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span><span class="p">)</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>The next update we need to make is in our <code>handleSubmit</code> function:<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code> <span class="kd">const</span> <span class="nx">handleSubmit</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">()</span> <span class="nx">setFinalAffirmation</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>If we hover over the <code>e</code>, we get the same message we did above. You might be tempted to type it in the same way, but let’s think about what’s happening here for a second. We’re submitting a completed form here. If we head over to the <a href="https://react-typescript-cheatsheet.netlify.app/docs/basic/getting-started/forms_and_events/#list-of-event-types">React TypeScript Cheatsheet</a>, we’ll find an event type of <code>FormEvent</code>, which they describe as an “Event that occurs whenever a form or form element gets/loses focus, a form element value is changed or the form is submitted.” That sounds like a good choice.<br> </p> <div class="highlight js-code-highlight"> <pre class="highlight javascript"><code><span class="cm">/* submit is a form event */</span> <span class="kd">const</span> <span class="nx">handleSubmit</span> <span class="o">=</span> <span class="p">(</span><span class="nx">e</span><span class="p">:</span> <span class="nx">React</span><span class="p">.</span><span class="nx">FormEvent</span><span class="o"><</span><span class="nx">HTMLFormElement</span><span class="o">></span><span class="p">)</span> <span class="o">=></span> <span class="p">{</span> <span class="cm">/* prevent page from refreshing */</span> <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">()</span> <span class="nx">setFinalAffirmation</span><span class="p">(</span><span class="kc">true</span><span class="p">)</span> <span class="p">}</span> </code></pre> <div class="highlight__panel js-actions-panel"> <div class="highlight__panel-action js-fullscreen-code-action"> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-off"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div> </div> <p>We add a comment there for clarity that submit is a form event, and then we specify the type as an <code>HTMLFormElement</code>.</p> <p>We’ll move on to the last one in this example.</p> <p>Around line 57, we can see that TypeScript is complaining about <code>socketRef.current</code>. If we hover over it, we get this message:<br> <a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MZeL3Owj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uu1y0hn4dvb0xo76tlim.png" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MZeL3Owj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uu1y0hn4dvb0xo76tlim.png" alt="Type 'WebSocket' is not assignable to type 'null'" loading="lazy" width="880" height="151"></a></p> <p>Because we added the WebSocket types package, <code>@types/websocket</code>, we have access to a WebSocket type. However, we don’t set the type on line 57. The actual problem is with <code>const socketRef = useRef(null)</code>.</p> <p>We need to let TypeScript know that we’re looking at a WebSocket or null: <code>const socketRef = useRef<WebSocket | null>(null)</code>.</p> <p>With that last update, we no longer have TypeScript complaining in this file. That doesn’t mean that there aren’t other updates that we can make. If you hover over other variables or functions, we can see what TypeScript is inferring.</p> <p><a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4MI3Ycnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny009v1ukv7hxfz820uo.gif" class="article-body-image-wrapper"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4MI3Ycnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ny009v1ukv7hxfz820uo.gif" alt="gif hovering over code" loading="lazy" width="880" height="357" data-animated="true"></a></p> <p>To complete this migration, we need to update the .js files to .tsx. But the nice thing is that we don’t have to do that all at once. For now, this is where we’ll leave it.</p> <h2> <a name="learn-more-about-typescript" href="#learn-more-about-typescript"> </a> Learn More About TypeScript </h2> <p>There’s a lot to learn about TypeScript. Here are some resources I've found helpful:</p> <ul> <li><a href="https://www.typescriptlang.org/docs/handbook/intro.html">TypeScript Handbook</a></li> <li><a href="https://www.totaltypescript.com/tutorials/beginners-typescript">Totally TypeScript</a></li> <li><a href="https://github.com/typescript-cheatsheets/react#reacttypescript-cheatsheets">React + TypeScript Cheatsheets</a></li> </ul> <p>And check out the Bonus Section of Sandra’s post on how to <a href="https://blog.deepgram.com/build-a-todo-list-with-pinia-and-vue-3/#bonus-section:-add-typescript">Build a To-do List App with Pinia and Vue 3</a>.</p> <p>To access the code for this blog post, select the <code>feature/typescript</code> branch from the <a href="https://github.com/deepgram-devs/react-app">React App GitHub Repository</a>. If you have any thoughts, comments, or questions, please drop a comment in our <a href="https://github.com/orgs/deepgram/discussions/categories/general">General Discussion category</a>, or if you want to say hey, drop your intro in our <a href="https://github.com/orgs/deepgram/discussions/3">intro thread</a>.</p>

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts