ASP.NET Core,  React,  Typescript,  Webpack

React with Typescript, Get started with ASP.NET Core

React with Typescript is part 2 of a 5 parts React with ASP.NET Core series. This series will show you the setup necessary for building React single page application with routing and server-side rendering.

When I first dived into migrating from traditional server side rendering towards more focus on client side rendering using React, one major decision to make was to whether go with javascript or typescript. The confusion at the time was really why we need another layer of technology on the already familiar javascript we have been using for many years. Isn’t client side rendering already much more complicated than we want already and here we have another layer of complexity.

Why Typescript

Check TypeScript Deep Dive out as for Why TypeScript. It also has a lot of other resources about Typescript.

I have been using many strongly typed languages like C++, C#, Java, Swift for many years and also enjoy the flexibility javascript provides. While still with some skepticism, I decided to move forward with Typescript. I haven’t looked back.

We really benefit from catching a lot of errors/potential bugs during development with type checking. And more often than not, when we did hit some bugs, those are the break points when we were getting lazy and try to skip some type checking using the type any. That’s when we got those undefined objects and causing errors. While defining types in Typescript could be tedious and sometimes hard to comprehend when the types are complex, I preferred this tedious over those undefined objects or hidden bugs that could drag you for nights.

There were also a problem with types compatibility. There may be some older libraries you want to use but do not have type definitions. Or there could be type conflicts with Visual Studio’s Typescript support. These issues can drive you crazy. But Typescript support has since improved a lot and is getting better and better.

React with Typescript, Get Started

Goal: Use react with typescript to display a simple HelloWorld component.

The setup will base off on the example codes from part 1 React with ASP.NET Core, Get started using Visual Studio . With this base project codes, we can already run React components with ASP.NET Core with basic webpack settings. It has project structure as shown below:

React with Typescript: project structure

First we enable Typescript for React. Then go to menu  Menu Project > [Project name] Properties... , select TypeScript Build  tab, and update as the screen shown:

React with Typescript: typescript settings

Note: Both Visual Studio & webpack will compile typescript files (i.e. each file is compiled twice and Visual Studio compiled files are not used for bundling). We can stop Visual Studio from compiling typescript files. However, doing so will also disable syntax checking of typescript during editing. Not sure if there’s a way to get around this. If you want to keep syntax checking, skip to the next part.

To disable typescript compilation by Visual Studio, go to Project > Edit [Project name].csproj, add the following lines inside the Project tag.

Add Typescript version of HelloWorld

Create HelloWorld.tsx & entry.tsx, using file type Installed > Visual C# > ASP.NET Core > Web > TypeScript JSX File.

Update Webpack to handle Typescript

Add the two packages to package.json inside devDependencies

Webpack will use ts-loader to convert typescript files ( .tsx/.ts) to .jsx/.js format

Next ts-loader needs a typescript config file tsconfig.json. Add it under your project

Finally, modify the webpack.config.js as shown to use the ts-loader:

The project should now be ready to build and run react with typescript. (VS Build and  Task Runner Explorer > Run - Development)

Example code for React with ASP.NET Core series – Part 2 is available at Github.

More Read

React with ASP.NET Core series – Part 3: React with Redux, Get started, with ASP.NET Core

React with ASP.NET Core series – Part 1: React with ASP.NET Core, Get started using Visual Studio

Leave a Reply

Your email address will not be published.