Create-React-App with TypeScript – A new way of learning web development

Table of Contents

If you’re a React developer, you might have heard of TypeScript, the statically typed superset of JavaScript that can make quicker, more vigorous React apps. Create React App Typescript cooperate in lightening some downsides of React.

Create React App with TypeScript

Typescript’s refined type framework accompanies somewhat of an expectation to learn and adapt, so today, we need to walk you through all that you want to know to begin.

Today we will examine:

  • Introduction
  • How does Create React App react
  • Install Create React App
  • Outline of Typescript and React
  • TypeScript is the missing piece of Javascript
  • Benefits of using Typescript with React
  • Project Creation
  • Adding Typescript to the React application
  • React <3 TypeScript
  • What to learn now
  • Conclusion
  • FAQs

Introduction

  • React

React is a JavaScript library mainly used by the react js development company that works in assisting developers with building user interfaces or UIs. As far as sites and web applications, UIs are the assortment of on-screen menus, search bars, buttons, and whatever else somebody collaborates with to use a site or application.

It was brought in use to handle the view layer and develop both web and mobile applications.

  • Typescript

TypeScript(TS) is a form of a typed superset of JavaScript(JS) that gives accumulated time checking of source code and has a lot of other decent elements to native JS. TS assists us with fixing most bugs and furnishes us with integral tools to further develop code quality. 

How Does Create React App React

Create React App is a tool made by Facebook developers that assists with bootstrapping a new ReactJS project by creating a new single-page application with a basic configuration with one order.

It improves the processes involved with setting up a new application and makes it significantly less tedious. Particularly useful for beginners who want to create a project and don’t want to go further into setting such complicated things.

Create React App doesn’t make backend or set the database; it simply creates frontend so that you can easily use it with any sort of backend. It uses Webpack and Babel.

Install Create React App

Beforehand, it was expected to install the Create React App package worldwide on your computer using npm or yarn. However, it’s unnecessary at the present moment since when using npx, it will install the most up-to-date version for you consequently.

Along these lines, it’s not prescribed to install Create React App, and it’s to the point of using npx.

npx accompanies npm, and it’s accessible from version 5.2 and higher. npx is an npm package sprinter, and that implies it downloads and runs the package temporarily.

That is the reason when react js development company use Create React App, and it’s running just once per application lifecycle, we should use npx.

The react development services should figure out how to install the ReactJS application with Create React App.

Next, create react application.

When creating another ReactJS project with the Create React App tool, there are three choices. However, the most prescribed one is to use npx make react application, yet you can likewise do it using npm and yarn.

To make your ReactJS app in the most suggested manner, use the accompanying code:

 

npx create-react-app

 

Whenever you choose to make your ReactJS application with npm, do it like this:

 

npm create-react-app

 

With yarn, use the code beneath:

 

yarn create-react-app

 

If any of the techniques won’t work, or return a bug, ensure you have appropriate versions of the tools which are being used.

 

Create React App template is one of the pre-defined setups that comprises various libraries like React Router or Prettier. We want to pass an extra flag to the command while making a new project to use the Create React App template.

 

npx create-react-app – format <template-name]

 

Create React App will make your application using the base template if you select no template.

Within the Create React App templates, the names mentioned are always started with cra-template- , yet we don’t need to give the long name.

There are a few templates accessible on the internet, and it’s workable to make a custom one, as per your requirements. You can go to the npm site to track down templates and search for cra-format.

Outline - Typescript with React

The TypeScript is a superset of JavaScript, so any component of JavaScript is likewise accessible in TypeScript. However, TypeScript makes JavaScript one stride further and adds a powerful type system to empower code refactoring, navigating features, type checking; from there, the sky is the limit.

Typescript with React

This powerful statically-typed language is incredible all alone. However, it likewise does an excellent deal for React developers. Many React developers who take on TypeScript say they can’t envision their work without it.

By using React App Typescript, we want to know how Typescript treats JavaScript code. While a complete course on TypeScript is expected to raise you to an acceptable level, let’s dive into the information showing how TypeScript adds to JavaScript code:

 

  • Type annotations: permit us two allocate types to variables
  • Type inference: gathers the information type of variable
  • The never type: used to address a kind of value that won’t ever happen
  • The unknown type: permits us to reduce the use of any and make all the more specifically code
  • Intersection types: allow us to merge existing types to form a new kind with every one member of the kind it depends on
  • Union types: enables us to join another form of type using the pipe | character
  • Make types reusable with generics
  • Specifically-typed clusters/arrays
  • Specifically-typed tuples

TypeScript is the missing piece of Javascript

I have appreciated writing in TypeScript for over a year, and presently I believe I will battle without it.

If you are dealing with any task you need to keep up with for a more extended time frame, write in TypeScript.

 

Tip: Use Bit (Github) to seclude React TypeScript parts from your task and offer them to an assortment soon.dev. It would make your components accessible for reuse (all over the place) for yourself as well as your team. It’s an extraordinary method for joining React TypeScript in a current React JS project without refreshing the entire project!

Benefits of using Typescript with React

You might wonder, “but for what reason would it be advisable for me to involve Typescript for a React.js project?”. React development services should break down the benefits of Typescript and show how it makes frontend React development simpler.

  • Code suggestions: 

Typescript gives recommendations and choices while you type. This saves a ton of exertion and makes it simpler for a new developer to use your source code. These code suggestions forestall wasted time, ease team communication, and make your codebase more reliable.

  • Coherence and validation: 

Since Typescript is a statically typed system, you can add types to variables, functions, and properties, making your code far more straightforward to read. Typescript additionally takes out React’s PropTypes, which makes React app development more straightforward.

  • Get errors prior and IntelliSense: 

One of the most loved elements of Typescript is its capacity to highlight the mistakes when they crop up. Typescript shows you errors before you at any point run the code, saving long periods fixing bugs or errors from the get-go.

  • Accurate code support and refactoring: 

As the codebase of a React app grows, it can turn out to be too challenging to even think about reading, sharing, and maintaining. Exploring these codebases can be drawn out, and refactoring code is unsafe. Typescript helps you refactor code and forestall errors, making it far simpler to keep up with an update changing none of its ways of behaving.

  • Improves use of JavaScript: 

Typescript incorporates entirely with JavaScript and uses the new features which are available to a JavaScript developer. The projects of React built with Typescript compile to a version of JavaScript that runs on any browser.

Project creation

Along these lines, we should begin by creating a project. To create a simple React App Typescript project, it is suggested to use the create React app utility with the template parameter:

 

npx create-react-app <project_name> – – template typescript

 

From that point onward, you will have a React App Typescript project prepared to run, in which TS support is now configured, and you can quickly begin writing code.



  • Components

We should introduce a small Button component, which contains the accompanying logic:

  • Handles clicks
  • Saves and displays the number of clicks on the button

Also, here we, as of now, have a few issues:

Suppose we don’t pass a function to the component in the prop onClick or pass an alternate type of information there. In that case, the component will break at runtime (in the handleClick work), which you probably won’t see during development.

In the prop text, we can pass any value, which could prompt unexpected results.

Most of the issues are because we don’t know what kinds of information come to us in props. This issue is part of how the propTypes library addresses; however, it has its limitations – it can type the component’s props. TS permits you to cover everything with types: handleClick function, onClick function, additional assistants and utilities, stores, and so on. 



  • Working with API

 

It is likewise attractive to write interfaces to work with the API. Usually, it’s beneficial to describe React’s interface. Also, if your server is written in NodeJS using TS, you can once portray interfaces for React and use them both on the server and the client. It’s beneficial.

Adding Typescript to the React application

Below are the essential steps to add Typescript to a React app.



  • Installation: 

You’ll initially need to install React onto your PC. It is prompted that you don’t involve the global version as that doesn’t give the most recent variant of npx. Abstain from using the npm install – g create-react-app and on second thought, use a site to carefully and safely install React.



  • Create a new project: 

To make another venture run the accompanying commands –

 

npx create-react-app my-app – – template typescript

# or

yarn create-react-app my-application – – template typescript



  • Rename: 

 

After that, rename any document to TypeScript. Ensure your development server is up to date and restart the server. Errors will undoubtedly appear while typing in a similar console as the building one. Thus, you’ll have to design your coding and fix these mistakes before you continue with developing your project.

React <3 TypeScript

Even though the default project
generator for React works with JavaScript, Create React App Typescript is made
for one another. Assembling and using components is cleaner with TypeScript.

Typescript

There are two standard ways to create
a React project with TypeScript: manually using Create React App. Today, we
will tell you the best way to use Create React App to do a basic Create React
App Typescript project. 

 

Step 1: Gather technical prerequisites

To set up another React app with
Typescript, you will require the accompanying tools:



  • Node.js and
    npm. You will need version 5.2 at least.
  • A code editor.
    Visual Studio Code is a suggestion here.
  • Create React App (CRA)

 

Step 2: Set up React with Typescript

Create React App (CRA) is one of the
command-line tools used to build React applications rapidly. You can download
CRA as an npm package that uses the npx command. If the idea is to create a new
React app using create-react-app v2.1 or higher, Typescript is implicit. Along
these lines, to set up a new project with Typescript, basically use – –
Typescript as a parameter.

 

npx create-react-app hello-tsx – – typescript

 

This results in creating a project
called my-app, but you can change the name. It’s vital to note that files
containing JSX should be a .tsx file, and files with just Typescript code
should be a .ts file.

The app’s root HTML document is in
the public folder. It is called index.html. The React app is infused into
index.html by the following line of code:

 

ReactDOM.render(,
document.getElementById(‘root’));

 

This implies that the root React part
is App, characterized in App.TSX.

To add TypeScript to a current
create-react-app project, you’ll have to install TypeScript and any necessary
types. Before starting the server, you want to rename your records to .tsx or
.ts. This generates the tsconfig.json file.

 

Step 3: Run the application

Since you have the app all set, you
can run it being development mode. Find the project’s root folder and run the
start npm script in the Terminal.

Your root folder should have a
similar name as your application and contain the package.json file. You
presently have your React app all set with TypeScript!

What to learn after this

Now you have a basic comprehension of what Typescript is and what it brings to the table for your React projects. There is a considerable amount more than Typescript can accomplish for React; thus, significantly, there is more to learn! Next, you’ll want to know:

 

  • Create strongly-typed class components – It is commonly used to provide theme information, such as color value, so that all components have access to it.
  • React component props – one of the best parts of using Typescript with React is the use and availability of the strongly typed component props. With this, you can quickly prevent mistakes in your app.
  • Create React App Typescript project from scratch – you need to use npm to install all the required dependencies for the new TypeScript project with React support.
  • Using Babel and Webpack – a Webpack helper transforms your JavaScript dependencies with Babel.
  • Add npm scripts – This command is stored in the package. JSON, so you don’t need to memorize it and just type the usual npm run start in place of it. An npm start is a short form or the shortcut for the latter. 
  • and then some

Conclusion

If you’re looking forward to building a React application with Typescript, we have improved the whole interaction for you as a step-by-step guide above. You genuinely should follow these means to keep away from any errors and redundancies.

 

One can use Create React App Typescript together ideally by taking a touch of learning. The advantages will gigantically pay off over the long term. We took care of the typical use cases; the sky is the limit from there. If you’re now dealing with the issues in making a react application using TypeScript, comment below to let us assist you in walking ahead in every way possible.

FREQUENTLY ASKED QUESTIONS? (FAQ’s)

A typeScript is an integral tool that helps write more secure, self-archiving code, permitting developers to catch the bugs quicker.

To add and run TypeScript to a Create React App project, first install it and its respective @types:

  • npm install – – save typescript @types/hub @types/react @types/react-dom @types/jest.
  • yarn add typescript @types/hub @types/react @types/react-dom @types/jest.

You make a new React project with the command create-react-app, followed by the name of your project. For instance: create-react-app my-application.

You can follow the steps mentioned below to make a React app from scratch:

  • Create the project directory.
  • Install Dependencies.
  • Add src directory and index.js file.
  • Add file src/list. HTML.
  • Configure Webpack and Babel.
  • Add ‘start’ script to the package. JSON.
  • Test to see your application running in the browser.
Shopping Basket