<?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: Phil Smith</title>
    <description>The latest articles on DEV Community by Phil Smith (@smithphil).</description>
    <link>https://dev.to/smithphil</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%2F131587%2F763a36e7-29e0-4e05-b814-c6090570f4a6.jpeg</url>
      <title>DEV Community: Phil Smith</title>
      <link>https://dev.to/smithphil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smithphil"/>
    <language>en</language>
    <item>
      <title>Conditional Properties for React TypeScript Functional Components</title>
      <dc:creator>Phil Smith</dc:creator>
      <pubDate>Fri, 23 Apr 2021 16:00:00 +0000</pubDate>
      <link>https://dev.to/smithphil/conditional-properties-for-react-typescript-functional-components-fp0</link>
      <guid>https://dev.to/smithphil/conditional-properties-for-react-typescript-functional-components-fp0</guid>
      <description>&lt;h2&gt;
  
  
  Pattern Description
&lt;/h2&gt;

&lt;p&gt;A property or properties should only be present when another property has a specific value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Problem
&lt;/h2&gt;

&lt;p&gt;For example: Say you want three possible actions on a component, download, preview, and print, and you want to have buttons who's click events execute those actions. The actions are grouped as follows, the component will either allow the user to preview and print a PDF OR to download a PDF. &lt;/p&gt;

&lt;p&gt;You could make the methods optional and qualify for them at run time, but this defeats the purpose of TypeScript. Something like:&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;interface&lt;/span&gt; &lt;span class="nx"&gt;ActionComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;className&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="p"&gt;...&lt;/span&gt; &lt;span class="c1"&gt;// other properties go here&lt;/span&gt;
    &lt;span class="nx"&gt;purpose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;print&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;onDownload&lt;/span&gt;&lt;span class="p"&gt;?:()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;onPreview&lt;/span&gt;&lt;span class="p"&gt;?:()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;onPrint&lt;/span&gt;&lt;span class="p"&gt;?:()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;void&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;And then in your code you can wire up events to these with something  like ...&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&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;purpose&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&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;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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;onDownload&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="sr"&gt;/button&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;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;purpose&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;print&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
         &lt;span class="c1"&gt;// render print buttons wired to with props.onPreview and props.Print &lt;/span&gt;
     &lt;span class="p"&gt;)})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're using ! to force TypeScript to compile with the optional &lt;code&gt;props.onDownload&lt;/code&gt; method, we'll have to do the same for the print buttons, and we're assuming that the properties will be populated. In our parent component we can set the purpose property to "download" and not populate the onDownload property resulting in exactly the type of runtime error TypeScript is designed to avoid. There are other approaches that will also cause avoidable problems, such as using a ternary operator to qualify if &lt;code&gt;props.onDownload&lt;/code&gt; is populated and handling its absence at runtime, again defeating the purpose of using TypeScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;With TypeScript we can create conditional properties using custom types and discriminating unions. Create an interface with the common properties for the component&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;interface&lt;/span&gt; &lt;span class="nx"&gt;BaseProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;className&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="p"&gt;...&lt;/span&gt; &lt;span class="c1"&gt;// other properties go here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now create a type from a discriminating union, I'll explain how that works as we go along.&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;type&lt;/span&gt; &lt;span class="nx"&gt;PdfButtonProps&lt;/span&gt; &lt;span class="o"&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;purpose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;onDownload&lt;/span&gt;&lt;span class="p"&gt;:()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&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;purpose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;print&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;onPreview&lt;/span&gt;&lt;span class="p"&gt;:()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;onPrint&lt;/span&gt;&lt;span class="p"&gt;:()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;void&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;The type of PdfButtonProps is determined by the discriminating union between the two types. The discrimination occurs on the shared property, which is &lt;code&gt;purpose&lt;/code&gt;. You could think of it in terms of a ternary operator, and it equates to something 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;pdfButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;purpose&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;PdfDownloadButton&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;PdfPrintButtons&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we declare our functional component we can create a new type as an intersection of our BaseProps interface and our PdfButtonProps type, and use that as our functional component props (change this to suit your preferred approach to declaring functional components).&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;type&lt;/span&gt; &lt;span class="nx"&gt;PdfComponentProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BaseProps&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;PdfButtonProps&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;PdfComponent&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;FC&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PdfComponentProps&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="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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="c1"&gt;// other possible components&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;purpose&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="c1"&gt;// render download button wired with props.onDownload&lt;/span&gt;
        &lt;span class="p"&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;purpose&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;print&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="c1"&gt;// render print buttons wired with props methods&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;In parent component's code:&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&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;PdfComponent&lt;/span&gt; 
        &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;form-buttons-pdf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;purpose&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;onDownload&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onDownloadHandler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Compiles&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PdfComponent&lt;/span&gt;
        &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;form-buttons-pdf&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;purpose&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;download&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;onPreview&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onPreviewHandler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onPrint&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onPrintHandler&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Does&lt;/span&gt; &lt;span class="nx"&gt;not&lt;/span&gt; &lt;span class="nx"&gt;compile&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first instance compiles, but the reason the second instance of PdfComponent does not compile is because the type of PdfButtonProps with &lt;code&gt;purpose === "download"&lt;/code&gt; does not have an onPreview or onPrint property, and because the code doesn't provide for the onDownload property. If the first instance's purpose was set to "print" it wouldn't compile as there is no onDownload property for that type, and the onPrint and onPreview properties have not been provided.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/2/conditional-types.html"&gt;TypeScript Conditional Types&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html"&gt;TypeScript Union and Intersections&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>react</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Life If Everything Was JavaScript</title>
      <dc:creator>Phil Smith</dc:creator>
      <pubDate>Sat, 19 Dec 2020 11:22:29 +0000</pubDate>
      <link>https://dev.to/smithphil/life-if-everything-was-javascript-46bf</link>
      <guid>https://dev.to/smithphil/life-if-everything-was-javascript-46bf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7SXG4cSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/43pjo6bjs60kmhwj6f1v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7SXG4cSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/43pjo6bjs60kmhwj6f1v.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>jokes</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Visual Studio 2019 - ASP.NET Core 3.1 React App with TypeScript</title>
      <dc:creator>Phil Smith</dc:creator>
      <pubDate>Thu, 09 Jul 2020 12:56:19 +0000</pubDate>
      <link>https://dev.to/smithphil/visual-studio-2019-asp-net-core-3-1-react-app-with-typescript-26hi</link>
      <guid>https://dev.to/smithphil/visual-studio-2019-asp-net-core-3-1-react-app-with-typescript-26hi</guid>
      <description>&lt;p&gt;After some wrestling with Visual Studio 2019 I'm now developing my React App with TypeScript.  &lt;/p&gt;

&lt;p&gt;For my project I'm using ASP.NET Core 3.1, the project structure seems to be the same for previous versions of .Net core and also the .Net framework. When you first create the project you get a basic structure for an MVC RESTful API and a basic React app created using &lt;code&gt;create-react-app&lt;/code&gt; and JavaScript.&lt;/p&gt;

&lt;p&gt;We use TypeScript, so here's the steps that I took to move my project to a TypeScript codebase. I've installed TypeScript into the package.json myself and as I've found that the NuGet TypeScript compiler is problematic. We also use npm instead of yarn or another package managers.&lt;/p&gt;

&lt;p&gt;This post assumes the reader has a basic familiarity of JavaScript, TypeScript, React, and the tools used for developing with them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.nodejs.org"&gt;Node.js&lt;/a&gt; Version 12.18.2+ &lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://www.npmjs.org"&gt;Npm&lt;/a&gt; Version 6.14+ (npm is installed with Node.js)&lt;/p&gt;

&lt;p&gt;You can check to see if and what versions of node.js and npm you have installed by entering &lt;code&gt;node -v&lt;/code&gt; and &lt;code&gt;npm -v&lt;/code&gt;, respectively, in the terminal.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="(https://visualstudio.microsoft.com/)"&gt;Visual Studio 2019&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Steps for creating the project
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Launch Visual Studio 2019&lt;/li&gt;
&lt;li&gt;Select Create a new project&lt;/li&gt;
&lt;li&gt;Select ASP.NET Core Web Application&lt;/li&gt;
&lt;li&gt;Choose a project name and location and click on Create&lt;/li&gt;
&lt;li&gt;Select React.js with the default options and click on Create&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One the project has been created click on the run icon, this will build and run the project. You should eventually see the project home page in your browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switch to TypeScript
&lt;/h2&gt;

&lt;p&gt;Once you've built and run the project, you need to move it to TypeScript. To do that we have have to change some of the packages being used. My preference is to do this within Visual Studio but you could do it from a terminal running outside of Visual Studio or in another editor. To do it within Visual Studio select View &amp;gt; Terminal which open the a window at the bottom of the IDE, and then within that window select Developer Command Prompt.&lt;/p&gt;

&lt;p&gt;In the terminal navigate to the ClientApp sub-folder of the project and then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Upgrade the react scripts to the latest with &lt;code&gt;npm upgrade react-scripts --latest&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remove all eslinting packages with &lt;code&gt;npm remove eslint eslint-config-react-app eslint-plugin-flowtype eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react&lt;/code&gt;¹&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install or upgrade TypeScript&lt;/p&gt;

&lt;p&gt;To install TypeScript use &lt;code&gt;npm install typscript --latest --save-dev&lt;/code&gt;&lt;br&gt;
or if you already have TypeScript installed you can upgrade with &lt;code&gt;npm upgrade TypeScript --latest&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the TypeScript definition files with &lt;code&gt;npm install @types/node @types/react @types/react-dom @types/react-router&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have that complete, rename the App.js file to App.tsx. You'll find App.js under &amp;lt;project_root_folder&amp;gt;/ClientApp/Src/App.js. Once it's renamed run and build the project again, once you see it loaded in your browser you're good to go with TypeScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Troubleshooting
&lt;/h3&gt;

&lt;p&gt;If TypeScript complains about &lt;code&gt;allowSyntheticDefaultImports&lt;/code&gt; or &lt;code&gt;esModuleImport&lt;/code&gt; compiler flags² not being set, you may be able to solve that by doing the following, which I found on Stackoverflow &lt;a href="https://stackoverflow.com/questions/58729856/how-to-solve-can-only-be-default-imported-using-the-esmoduleinterop-flag-in"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In Visual Studio 2019 Solution Explorer right click on the project file and select 'Unload Project'&lt;/li&gt;
&lt;li&gt;When it has unloaded right click on the project file again and select Edit &amp;lt;ProjectName&amp;gt;.csproj&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In the text editor look for the lines the look something like this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ItemGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;None&lt;/span&gt; &lt;span class="na"&gt;Remove=&lt;/span&gt;&lt;span class="s"&gt;"ClientApp\src\public\App.tsx"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ItemGroup&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ItemGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TypeScriptCompile&lt;/span&gt; &lt;span class="na"&gt;Include=&lt;/span&gt;&lt;span class="s"&gt;"ClientApp\src\public\App.tsx"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ItemGroup&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remove these ItemGroups (they may have more than one tsx file referenced in each, depending on how much development you've done)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Save the file, and then close it&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In Solution Explorer right click on the file again and select 'Reload Project'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the project is reloaded, save all files and restart Visual Studio&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should be good to go&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Links
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://visualstudio.microsoft.com/"&gt;Visual Studio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.microsoft.com/en-us/aspnet/core/?view=aspnetcore-3.1"&gt;ASP.NET Core 3.1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/"&gt;React&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/"&gt;TypeScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/"&gt;Node.js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/"&gt;NPM&lt;/a&gt; - this is installed with Node.js on Windows&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Footnotes
&lt;/h4&gt;

&lt;p&gt;&lt;a id="footnote1"&gt;¹&lt;/a&gt; At the time of writing the article these linting packages were added to the package.json in the ClientApp folder by Visual Studio when the project was created. These may change with updates to either Visual Studio 2019 or the React App project template. &lt;/p&gt;

&lt;p&gt;&lt;a id="footnote2"&gt;²&lt;/a&gt; This may include other TypeScript compiler flags, but these were the only ones I encountered.&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>webdev</category>
      <category>dotnetcore</category>
    </item>
    <item>
      <title>Troublshooting npm proxy issues part 1</title>
      <dc:creator>Phil Smith</dc:creator>
      <pubDate>Tue, 03 Sep 2019 12:21:36 +0000</pubDate>
      <link>https://dev.to/smithphil/troublshooting-npm-proxy-issues-part-1-8l0</link>
      <guid>https://dev.to/smithphil/troublshooting-npm-proxy-issues-part-1-8l0</guid>
      <description>&lt;p&gt;The npm package manager is a must have for JavaScript devs but, sometimes, being on a corporate network means you have to navigate a proxy to access the packages. This is the first post dedicated to troubleshooting npm proxy issues. &lt;/p&gt;

&lt;p&gt;If you run, for example, &lt;code&gt;npm install --save-dev grunt&lt;/code&gt; on our home network grunt will be installed into your project as expected. If you are running on a network with a proxy server you may get an error like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm ERR! code ETIMEDOUT
npm ERR! errno ETIMEDOUT
npm ERR! network request to https://registry.npmjs.org/grunt failed ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The error text will then go onto to suggest that if you are behind a proxy you must set the 'proxy' config. This means you have to tell npm the address of the proxy on your network. &lt;/p&gt;

&lt;p&gt;There's actually two config settings you have to set: proxy and https-proxy, https-proxy being the address for TLS. Let's say that the proxy address is &lt;a href="http://proxy.example.com:8080"&gt;http://proxy.example.com:8080&lt;/a&gt; then simply enter&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm config set proxy http://proxy.example.com:8080&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;and &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm config set https-proxy http://proxy.example.com:8080&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;At this point npm should start working, or it might give you a 403 error. Dealing with that 403 error will be the subject part 2.&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional Stuff
&lt;/h3&gt;

&lt;p&gt;Config settings can be displayed using the folllowing commands. To display a list of the user config settings use &lt;code&gt;npm config ls&lt;/code&gt;, all config settings are displayed with &lt;code&gt;npm config ls -l&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This second command will also show you the location of you .npmrc file, which is your userconfig file. You can type your config settings directly into that file and save it instead of using cli commands, if you want too, I prefer the cli. Here's more details on &lt;a href="https://docs.npmjs.com/cli/config"&gt;npm config&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>npm</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
