DEVELOPMENT,TECHNOLOGY

React App Development using TypeScript

20 Sep 2021

 

JavaScript is a developer’s favourite, as we all know. But it is not free from errors, is it? When projects get longer and more complex, the probability of type errors from developers might climb.

The best software development companies will try to roll out error-free codes as much as possible. This is where TypeScript plays its part and helps coders in avoiding these probable pitfalls.

Let us now see how TypeScript helps developers in pushing projects smoothly in an error-free manner with its features.

What is TypeScript?

While running the codes, TypeScript adds static typing to JavaScript for identifying errors before running the code. Static typing implies the declaration of the type of value, function, etc. Thus, software developers can determine if the value we are passing to a function is valid or not.

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript can be run in any browser or any engine, whether it be Node JS or any JS engine. With the guidance of TypeScript, developers can find errors during compilation and not at the time of running the application.

TypeScript has two parts:

  • A syntax 
  • A transpiler

Type Checking in React /React Native

React and React Native generally have 3 types of static type checking, namely PropTypes, Flow, and TypeScript. 

PropTypes is a built-in type checker which allows you to check props on a component, and Flow is a static type checker developed by Facebook. However, TypeScript is superior to Flow, and here is why:

  • Import automatically using VS Code
  • Best auto-fixing linter
  • Error messages are notified better
  • The flow requires file annotation using with //@flow
  • Errors can get forgotten, and TypeScript can be helpful to check the types right field at the beginning.

How to add TypeScript to your React Native project?

  1. Run ‘npm install –save-dev typescript react-native-typescript-transformer @types/react @types/react-native to install necessary dependencies.
  2. This will add TypeScript, React Native TypeScript Transformer, and TypeScript typing to the project.
  3. Add a TypeScript configuration file name named tsconfig.json with the required content.
  4. Now add a configuration file named rn-cli.config.jds for the React Native TypeScript Transformer with the required content.
  5. After this, the package should expect .ts and .tsx files and should use react-native-typescript-transformer for compiling source code.

How to migrate to TypeScript?

You can migrate your React Native project to TypeScript by changing the files in the root folder and the node modules .js except for the index.js should be changed to .ts or .tsx, if the files contain JSX. Make sure your script files have these aforementioned file endings. This can be done progressively by converting the existing code base. You only have to create new files in TypeScript, while legacy codes in JavaScript.

How to create a React project with TypeScript?

Creating a React Native project with TypeScript can be done either manually or automatically. Here are the steps involved in creating a React project with TypeScript:

Step1: Technical requirements gathering

Make sure you are ready with the following tools:

  • Latest version Node.JS and npm
  • A code editor like Visual Studio Code
  • Create React App (CRA)

Step 2: Setting up React with TypeScript

  • Download CRA as a nnpm package using the npx command
  • Either, you can use React app versions with built-in TypeScript or by using –typescript as a parameter
  • A project is created, the name of which can be changed by you. Make sure that files containing JSX should be a .tsx file, and files that contain the only TypeScript should be .ts files.
  • The app’s root HTML file which is in the public folder called the index.html is injected with the React app using the standard line of code.
  • Rename your files to .tsx or .ts before starting the server, which will generate the tsconfig.json file.

Step 3: Running the app

  • Run your app in the development mode. Identify the root folder of your project and run the start npm script in the terminal
  • Make sure that your root folder has the same name as your app and has the package.json file

You now have a TypeScript-enabled React app.

What are the pros of TypeScript in React Native?

Self-documentation:

You can write and maintain types you are documenting with values you expect in a function or component.

Simpler debugging:

TypeScript can tell what you did wrong and enables easier debugging. There will be no pass of a true string when you were expecting a boolean with TypeScript in place.

Easy refactoring:

TypeScript will help you with renaming a function or updating an argument, just in case you have forgotten.

A text editor or IDE  integrated: 

TypeScript is integrated with several editors that allow developers to define, autocomplete and tell you what to pass to a function or a component.

Larger apps can be structured: 

TypeScript can help you in defining and structuring how your app works and helps in avoiding any potential lock-ins your app can get into.

More guarantee to JavaScript:

With TypeScript, you will get even more guaranteed and valid JavaScript that will run on any target level you specify. Straight JavaScript, however, is not as valid or guaranteed as the TypeScript-powered JavaScript.

Easy adoption:

While writing codes, you don’t have to write 100% TypeScript. Adoption of TypeScript can be done even after testing waters, as valid JavaScript is still valid TypeScript.

Endnote:

TypeScript has gained immense popularity among developers over the past few years. Here are more reasons for you to stick to TypeScript:

  • Components are easy to read and understand 
  • JSX is better supported
  • Common libraries are supported by default TypeScript
  • Existing projects can gradually adopt it
  • Static type checking and improved IntelliSense

So, what are your thoughts? Are you going to add TypeScript for your next React project, or do you want to migrate your existing React project to TypeScript? For hassle-free coding, you can always approach the React Native app developers at Webdura. Let’s enjoy a happy coding time together for making our React apps error-free! 

smile

Thanks For Reading!

Webdura Technologies

Webdura Technologies

Webdura technologies is a full spectrum technology company in India with over 10 years of experience in developing technological solutions using ​JavaScript (ES6+), React JS, React Native, Redux, Rematch, Vue JS, Graph QL, Apollo, Meteor JS, Node JS, Gatsby JS, PHP, Wordpress, MySQL, Mongo DB and other latest tools. Webdura technologies have joined hands with many international and national giants to put forth cutting edge applications in this past decade.

Comments

POST YOUR COMMENTS

Sign up for our newsletter the monthly updates

How about a lil' game of fill in the blanks?

We love working alongside ambitious brands and people