<?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: Liam Lindner</title>
    <description>The latest articles on Forem by Liam Lindner (@refactornator).</description>
    <link>https://forem.com/refactornator</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%2F565417%2F346b58a7-a6ab-4639-98b8-a1bcac2decf2.jpeg</url>
      <title>Forem: Liam Lindner</title>
      <link>https://forem.com/refactornator</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/refactornator"/>
    <language>en</language>
    <item>
      <title>React + TypeScript = ❤️</title>
      <dc:creator>Liam Lindner</dc:creator>
      <pubDate>Mon, 25 Jan 2021 19:14:34 +0000</pubDate>
      <link>https://forem.com/refactornator/react-typescript-3g2</link>
      <guid>https://forem.com/refactornator/react-typescript-3g2</guid>
      <description>&lt;p&gt;TypeScript is a useful tool for cutting down on unnecessary tests and building more confidence in your codebase. When paired skillfully with React, you can prevent errors caused by passing the wrong type of data to React components. You won't need to write tests for errors that the compiler can catch. And you will get productivity benefits by leveraging refactor tools and code completion in your IDE.&lt;/p&gt;

&lt;p&gt;First, let's talk about TypeScript (TS). TS is a superset of JavaScript (JS), so everything you can do in JS you can do in TS with the addition of types. The TS compiler will not compile your code if you pass a variable with the wrong type into a function. Typescript does not fix your bugs, but when wielded correctly it greatly improves developer productivity and eliminates the need for unit tests that check types.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript
&lt;/h2&gt;

&lt;p&gt;A simple function that adds two numbers.&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;second&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="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;second&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;JavaScript lets you pass anything into the &lt;code&gt;add&lt;/code&gt; function and has no indication of what type comes back. This commonly results in errors, which leads to writing tests that the TS compiler could handle.&lt;/p&gt;

&lt;h2&gt;
  
  
  TypeScript
&lt;/h2&gt;

&lt;p&gt;A simple function that adds two numbers, this time with type annotations for the parameters.&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="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;second&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;Your IDE can read type annotations and provide helpful code completion.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkrc2lc09v43dpalmiy76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fkrc2lc09v43dpalmiy76.png" alt="Code Completion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your IDE can show compiler errors when the wrong type is passed in.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyyx1kg211mvpua6o3e3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fyyx1kg211mvpua6o3e3j.png" alt="Type Errors"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  React
&lt;/h1&gt;

&lt;p&gt;React is a popular UI library that uses the component pattern to separate your code into reusable chunks. You can think of most React components as functions. Functions take in parameters and return a value. A React component takes in props and returns other React components or DOM elements. Those props are where TypeScript comes into play. Each component has an API. Maybe it takes in a single string, maybe it takes in an array of objects, maybe some props are optional and others are required. Without TypeScript, you have no assurance that the props you are passing in are valid. When you invoke each component you’ll have to dig into the implementation just to know what types it expects.&lt;/p&gt;

&lt;p&gt;Below are two examples. The first is what you get when you write your React components in JavaScript. You can read the component to figure out which props are optional and what types they need to be, but this approach is cumbersome and prone to bugs.&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/typescript-prop-passing-in-react?file=JSExample.jsx&amp;amp;view=editor" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here is an example of using TypeScript. We declare the &lt;code&gt;TSExampleProps&lt;/code&gt; interface and define the &lt;code&gt;title&lt;/code&gt; prop as an optional &lt;code&gt;string&lt;/code&gt;, the &lt;code&gt;time&lt;/code&gt; prop as a &lt;code&gt;Date&lt;/code&gt; object, and the &lt;code&gt;items&lt;/code&gt; prop as an &lt;code&gt;array&lt;/code&gt; of &lt;code&gt;strings&lt;/code&gt;.&lt;br&gt;
&lt;iframe src="https://stackblitz.com/edit/typescript-prop-passing-in-react?file=TSExample.tsx&amp;amp;view=editor" width="100%" height="500"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmsn6xjjr0edjy0y3eqhb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmsn6xjjr0edjy0y3eqhb.png" alt="Compiler enforced API"&gt;&lt;/a&gt;&lt;/p&gt;
Your IDE complains when you pass the wrong types in



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvk6e8xx7159vyseavnhj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvk6e8xx7159vyseavnhj.gif" alt="IDE Code Completion"&gt;&lt;/a&gt;&lt;/p&gt;
Your IDE gives useful code completion hints



&lt;p&gt;When following this pattern, you can leverage refactoring tools that your IDE provides to quickly change any React component's API. The cost of maintenance goes down because the TS compiler catches bugs before you can write them. And you don't need a bunch of brittle unit tests. Give TypeScript + React a try and let me know in the comments below what additional questions you have.&lt;/p&gt;

&lt;p&gt;If you’re familiar with types and want to leverage them in your next react project, check out Create React App’s official support for TypeScript when bootstrapping a project: &lt;a href="https://create-react-app.dev/docs/adding-typescript/" rel="noopener noreferrer"&gt;https://create-react-app.dev/docs/adding-typescript/&lt;/a&gt;&lt;/p&gt;

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