OpenReplay
Navigate back to the homepage
BLOG
Browse Repo
Back

Using Typescript in React: a Crash Course

Ajibade Ola-Oluwa
June 19th, 2022 · 9 min read

Typescript is a strict superset of the Javascript programming language, extending Javascript with new features and syntax.

Typescript is like a built-in unit test and gives us the benefit of the compiler watching our back, prompting errors even before the code is run, and adding static and robust typing to our applications.

This article is a quick guide to get you started and comfortable using Typescript in React, creating simple use cases with Typescript. You’ll need an understanding of React, familiarity with Javascript Types, and basic understanding of Node and npm.

What is Typescript?

Typescript is an open-source programming language created by Microsoft, and it was designed to develop large applications by adding static typing controls to JavaScript. It may be used to develop applications for both client-side (web-browser) and server-side (Node.js or Deno).

In simple terms, Typescript is Javascript plus static typing features whenever you want them. The idea of having static type checks in Javascript is amazing; that is one reason why the use of Typescript is rapidly increasing.

Why do Software Companies prefer to use Typescript?

The use of Typescript has soared compared to the last six years ago, according to several rankings like this, this accounts.

Using Typescript (instead of Javascript) makes it possible to remove certain software anomalies earlier during the development phase and secure the code before going into production.

Typescript also makes it easier to reuse code and manage types and special cases, which the development team would not have thought of easily. It forces the team to take these topics into account, and as a result, best practices are encouraged, saving development time and reducing the frequency of bugs.

Learning Typescript is worthwhile when a team is more concerned about security. Typescript type checks codes at compile time, detecting possible issues before they become vulnerabilities, making it easier to trace application errors or weaknesses.

Adding Typescript to React

To create a new React project with Typescript template append
--template typescript to the npx create-react-app command. Run this on the terminal; it creates a new react app called my-app.

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

Using this command, the directory structure can be viewed as:

Directory structure

Run npm start on the terminal, to test that everything is working.

How does TypeScript compile React code?

Open the project directory in your preferred editor (I’m using VSCode here). You will notice a tsconfig.json file. This is where the magic of Typescript happens. Typescript looks for the tsconfig.json file in the project root folder, and that file provides configuration options for the compiler.

Typescript compiles our React code to type-check it. The v8-engine, which is the program that executes Javascript in the browser, does not understand Typescript, so transpilation is needed.

To run our app, Typescript is converted into plain Javascript by the Typescript compiler (tsc). The compiler goes through the src directory in our project and converts all .ts files into .js files. By default, it ignores files not in the src directory, but this default configuration can be modified in the tsconfig.json file.

Understanding Types

Before we delve into building React applications with Typescript, let’s talk about an essential part of Typescript, which enables its static type checking abilities. Generally, programming languages fall into two categories: statically typed or dynamically typed. In static typing, types of variables must be known at compile time. If a variable is declared, the compiler must know the variable type. Some languages in this category are C# and Java. In dynamic typing, the type of a variable is known only when the program is running. Python and JavaScript are languages in this category. Using Typescript converts JavaScript into a statically typed language.

What are types? They are labels that describe the different properties and functions a value has. Javascript has a number of built-in (primitive) types, all accessible to Typescript: string, number, boolean, null, undefined, etc. Typescript also has some advanced types:

  • void: denotes the absence of any type.
  • empty type {}: describes an object that has no property of its own.
  • tuple: works like an array, but the number of elements here is fixed. The types of elements in a tuple are known and can be of different types.
  • enum: is a group of constant values that are closely related and known.
  • any: allows us to assign the value of any type to a variable. Used when a value type is unknown
  • never: is a type that contains no value, so we can’t assign any value to a variable with a never type.
  • union: this describes a value that can be one of several types number | string | boolean
  • object type: To define an object type we list its properties and their types: {x:number, y:number}

Type annotations

Typescript uses its type annotation to specify the type of a value (string, boolean, number, etc). Typescript uses the syntax : type after a value’s identifier, where type is a valid type such as string, boolean, number, etc.

1let variableName: type;
2
3let name: string= 'Jane';
4const active: boolean= true;
5let arrayName: number[] = [2, 4, 6, 8]

Once a value identifier is annotated with a type, it can only be used as that type. If the identifier is used with a different type, tsc will issue an error.

Type Interfaces

Typescript interfaces are essential to enforcing a particular object shape. An interface is like an object; it contains information about the object’s properties and types. An interface explicitly tells the Typescript compiler about the property names and the value types an object can have.

1interface Location{
2 from: string;
3 to: string;
4}
5
6function printCoord(holiday: Location) {
7 console.log("We are going to" + from + "to" + to);
8}
9
10printCoord({from: "london", to: "new-york"});

After defining the Location interface, we can use it as a type. Here we annotate it to the function parameter to type-check the function arguments when called.

There are several ways typescript interface can provide type information in our codes.

Type Aliases

Aliases allow us to create a new name for an existing type. Here a new type is not defined; we only create a new name for the type. It is common to use the same type more than once, so a type alias is a convenient way to give a new name to an object type declaration.

Unlike an interface, type alias can also be used for other types such as primitives, unions, strings, and tuples.

Type aliases and interfaces are somewhat similar. Type interfaces work better with objects and method objects, while Type aliases work better with functions, complex types, etc.

For creating new type aliases, we use the type keyword.

1type Location= {
2 from: string;
3 to: string;
4}
5
6function printCoord(holiday: Location) {
7 console.log("We are going to" + from + "to" + to);
8}
9
10printCoord({from: "london", to: "new-york"});

Generics

Generics help make the type system flexible and reusable. They allow us to generically define types that a function can return with, using type-variables such as <T>. T is a placeholder and can be changed to any other variable name.

Why do we need Generics? Let’s take a look at a regular typescript function without generics.

1function removeItemFromArray(
2 arr: Array<number>,
3 item: number
4 ): Array<number> {
5 const index = arr.findIndex((i) => i === item);
6 arr.splice(index, 1);
7 return arr;
8 }
9
10 console.log(removeItemFromArray([1, 2, 3], 2)) //  [1, 3]

Above, we defined a function that removes a specified number from an array when called. Assuming we need to remove a string from the Array, we would have to develop a new function.

1function removeItemFromArray(
2 arr: Array<string>,
3 item: string
4 ): Array<string> {
5 const index = arr.findIndex((i) => i === item);
6 arr.splice(index, 1);
7 return arr;
8 }
9
10 console.log(removeItemFromArray(["red", "green", "blue"], "green")); //  ['red', 'blue']

The logic for both functions is the same. Later we may decide to remove another value type (boolean, object…) from an array, and we’d need yet more versions of the same logic… but creating a new function each time we want to remove a value from an array doesn’t scale.

One solution is to use the ‘any’ type; to do this, we need to set the type of the array argument to any[]. But the drawback here is that any doesn’t enforce the type of the returned element, which means it is not type-safe. A better solution to avoid creating multiple functions is to use Generics in typescript.

Using generic types to create a new removeItemFromArray() function would be as follows:

1function removeItemFromArray<T>(arr: Array<T>, item: T): Array<T> {
2 const index = arr.findIndex((i) => i === item);
3 arr.splice(index, 1);
4 return arr;
5 }
6
7 console.log(removeItemFromArray<string>(["red", "green", "blue"], "green"));
8 console.log(removeItemFromArray<number>([1, 2, 3, 4, 5, 6], 3));

Here we can notice the function uses the type variable T. T lets us capture the type provided when the function is called. The function also uses the type variable ‘T’ as its return type.

Now our function can be used with different types. In the above example, we can call the function explicitly by passing a type(number, string, etc.) as generic to the removeItemFromArray() function.

Conditional Types

Conditional type helps describe the relationship between types by using the extend keyword and selecting one of two possible types. Conditional types look like the ternary operator in Javascript (condition ? trueExpression : falseExpression ). They work great with generics.

1someType extend anotherType ? trueType : falseType;

The syntax someType extends anotherType describes the type relationship between them. If this condition is met, the trueType is selected; else, the falseType is selected.

Example for Conditional Types:

1interface Animal {
2 adopt() : void;
3}
4
5interface Dog extends Animal {
6 woof() : void;
7}
8
9interface Cat {
10 meow() : void;
11}
12
13type Owner1 = Dog extends Animal ? boolean : string; // returns boolean type
14
15type Owner2 = Cat extends Animal ? boolean : string; // returns string type

Here Dog extends Animal; therefore, it returns a boolean type, while the Cat interface is not extended to Animal. So for Owner2, a string is returned.

Functional components

A React Functional Component is a function that takes props objects and returns JSX elements. In React components, we need to consider the type of props coming in. We will be writing all code in the App.tsx file to make things simple to follow. Delete everything inside the function in that file and return the simple code below.

1import React from "react";
2import "./App.css";
3
4function App() {
5 return <h1>Hello World</h1>;
6}
7
8//add "Employee" component .....
9
10export default App;

There are several ways of creating a functional component in React using Typescript.

Props Type as an Argument

This is one of the easiest ways to declare a functional component. First, we state the type using the interface or (type) keyword. Then annotate the interface object to the function props. The interface object explicitly states the props type and name.

Introduce a new component, Employee, and call it inside our App component above it.

1import React from "react";
2import "./App.css";
3
4//type declaration
5interface MessageProps {
6 message: string;
7}
8
9function App() {
10 return (
11 <>
12 <Employee message="Hello" />
13 </>
14 );
15}
16
17// "Employee" component
18function Employee({ message }: MessageProps) {
19 return (
20 <>
21 <h1>Welcome {message}</h1>
22 </>
23 );
24}
25
26export default App;

Inline Type Declaration

Another way to create a functional component in React using Typescript is to inline a type declaration in the function component. This eliminates naming the props type but gets uncomfortable if the function component gets complex. Change the Employee component in the previous code to:

1// change "Employee" component
2function Employee({ message }: { message: string }) {
3 return (
4 <>
5 <h1>Welcome {message}</h1>
6 </>
7 );
8}

React.FunctionComponent or React.FC

A common way of creating a functional component in React using Typescript is to import React’s FunctionComponent or FC type; FC is just an alias for FunctionComponent. With this method, we cannot create the component using a function declaration, so we will have to use a function expression.

Change the Employee component in the previous code to:

1// change "Employee" component
2const Employee: React.FC<MessageProps> = ({ message }) => {
3 return (
4 <>
5 <h1>{message}</h1>
6 </>
7 );
8};

Using React.FunctionComponent or React.FC approach comes with some disadvantages:

  • Imply type of children: React.FC provides access to props’ children even when we don’t want it. Our App function now accepts a children prop, even though we are not doing anything with it.
  • Type a function, not its arguments: Using React.Fc types, we will have to annotate the function type itself. This means we need to use a function expression like an anonymous function assigned to a const/variable or arrow function when using React.Fc like in the example above.
  • React.FC doesn’t fit well with Generics.

React.FC can be helpful for beginners just getting into Typescript in React as it guides us with types. But it is not generally advisable to use React.FC today because of its disadvantages.

Open Source Session Replay

OpenReplay is an open-source, session replay suite that lets you see what users do on your web app, helping you troubleshoot issues faster. OpenReplay is self-hosted for full control over your data.

replayer.png

Start enjoying your debugging experience - start using OpenReplay for free.

Types for State and Props in React

In Typescript, variables, states, and props are limited to the types they are defined with; when we write our code. These types can be defined on the variable values using type annotations with primitive types, type interface, type alias, tuples, union, etc.

1import React, { useState } from "react";
2import "./App.css";
3
4interface IProps {
5 staff: string;
6 message: string;
7}
8
9interface IState {
10 position: string;
11 experience: number;
12}
13
14function App() {
15 return (
16 <>
17 <Employee message="Good Morning" staff="Ryan" />
18 </>
19 );
20}
21
22function Employee({ staff, message }: IProps) {
23 let [state, setState] = useState<IState>({
24 position: "Software Engineer",
25 experience: 17,
26 });
27 return (
28 <div className="App">
29 <h2>
30 {message} {staff}
31 </h2>
32 <ul>
33 <li>
34 <span>Position:</span> {state.position}
35 </li>
36 <li>
37 <span>Year Of Experience:</span> {state.experience}
38 </li>
39 </ul>
40 </div>
41 );
42}
43
44export default App;

Notice the use of interface to specify our state and props object types, and the type annotations used in the code. This is a typical pattern for type-checking state and prop values.

Event and Input form handling

Here is a tricky one, how do we get to know or specify the type passed to an event on change or input? React provides many built-in event types that we can use to specify the type for all events. One easy way to tell the type of event to use for a particular event is to hover on the event handler (e.g., onChange, onSubmit, onBlur).

Getting types for events

Hover on an event, and you’ll see the appropriate type for the event. Let’s see a quick example of a form using the event type in react:

1import React, { useState } from "react";
2import "./App.css";
3
4type IProfile = {
5 username: string;
6 password: string;
7};
8
9function App() {
10 const [profile, setProfile] = useState<IProfile>({
11 username: "",
12 password: "",
13 });
14
15 //Checking Event Type for onChange Method
16 let updateInput = (event: React.ChangeEvent<HTMLInputElement>): void => {
17 setProfile({
18 ...profile,
19 [event.target.name]: event.target.value,
20 });
21 };
22
23 //Checking Event Type for onSubmit Method
24 let login = (event: React.FormEvent<HTMLFormElement>) => {
25 event.preventDefault();
26 console.log(profile);
27 };
28
29 return (
30 <div className="App">
31 <h2>Employee Login</h2>
32 <form onSubmit={login}>
33 <div>
34 <input
35 name="username"
36 value={profile.username}
37 onChange={updateInput}
38 type="text"
39 placeholder="Username"
40 />
41 </div>
42 <div>
43 <input
44 name="password"
45 value={profile.password}
46 onChange={updateInput}
47 type="password"
48 placeholder="Password"
49 />
50 </div>
51 <input type="submit" />
52 </form>
53 </div>
54 );
55}
56
57export default App;

JSON Documents

Imagine a scenario where we are to work with a complex JSON object, like fetching an API and still having to use Typescript to type-check its data. This can be difficult; if possible, most programmers would rather avoid it.

One quick and secure way of using types for JSON data (API response) is to convert them to Typescript interfaces using tools like quicktype, transform, etc.

Getting types for JSON

To use the tools above, copy and paste the API JSON object into the tool, and it auto generates the interface type for us. Let’s see an example using quicktype.io to convert our JSON data into types for type-checking:

1import React, { useEffect, useState } from "react";
2import axios from "axios";
3import "./App.css";
4
5// Generated interface type from "quicktype.io"
6interface User {
7 userId: number;
8 id: number;
9 title: string;
10}
11
12function App(): JSX.Element {
13 const [employees, setEmployees] = useState<User[]>([]);
14
15 useEffect(() => {
16 axios
17.get("https://jsonplaceholder.typicode.com/albums")
18 .then((res) => setEmployees(res.data));
19 }, []);
20
21 return (
22 <div className="App">
23 <h1>COB Summary</h1>
24 <table>
25 <thead>
26 <tr>
27 <th>Id</th>
28 <th>Job Summary</th>
29 </tr>
30 </thead>
31 <tbody>
32 {employees.length > 0 &&
33 employees.map((employee) => {
34 return (
35 <tr key={employee.id}>
36 <td>{employee.id}</td>
37 <td>{employee.title}</td>
38 </tr>
39 );
40 })}
41 </tbody>
42 </table>
43 </div>
44 );
45}
46
47export default App;

Promises

Another Javascript feature extended to Typescript is the Javascript promise. In Typescript, the return type of promise is defined immediately after the promise keyword, determining the data type of the value returned by the promise when it is resolved.

Since errors can take any shape, the default data type of value returned when the promise is rejected is set to any by Typescript. Let’s look at the following example of using promises in React.

1const pickEvenNumber = new Promise<number>((resolve, reject) => {
2 const isEvenNumber = Math.floor(Math.random() * 20);
3 if (isEvenNumber % 2 === 0) {
4 resolve(isEvenNumber);
5 return;
6 }
7 reject(isEvenNumber);
8 });
9
10 pickEvenNumber
11 .then((res) => console.log("Resolved: Even Number " + res))
12 .catch((err) => console.log("Rejected: Not Even Number ", err));

In the above example, pickEvenNumber is a promise created using the promise constructor that resolves a number. The resolved data type of this promise is a number; hence tsc won’t allow us to call the resolve method with any other value except a number type.

Conclusion

In this article, we used Typescript in React to create functions, states, props, promises, and much more. Understanding Typescript is definitely an ace up your sleeve in your developer career.

For more in-depth knowledge on using Typescript with React, visit the documentation. You can find the article’s source code here.

newsletter

More articles from OpenReplay Blog

Forever Functional: Many flavors of currying

An explanation of currying: an important concept that provides benefits to JS developers

June 18th, 2022 · 6 min read

Stilify vs Master UI/Styles: next-generation Tailwind-like CSS libraries

Comparing two newcomers to the well known Tailwind CSS library

June 17th, 2022 · 6 min read
© 2022 OpenReplay Blog
Link to $https://twitter.com/OpenReplayHQLink to $https://github.com/openreplay/openreplayLink to $https://www.linkedin.com/company/18257552