Next, rename any file to be a TypeScript file (e.g. src/index.js to src/index.tsx). Type errors will start to show up.
Generate tsconfig.json file
Although the React documentation says that you are not required to make a tsconfig.json file because one will be made automatically for you, that was not my experience. After doing the installation, a tsconfig.json file was not generated for me.
I had to run the following command to generate the file:
npx tsc --init
Now, you should be able to run your React project with TypeScript, if there are no TypeScript errors.
This error comes from the use of Array without any type for the elements in the array. To solve this error, define a type for the contents of the array. You can define this in 2 different ways:
Solution 1:
clients: String[];
Solution 2:
clients: Array<String>;
If the type of the array can’t be determined, use any:
any[]
Array<any>
Note: Using the T[] syntax is recommended over Array<T> syntax
In this article, we will create a simple React component and do some basic testing on the component using React Testing. This will help you get acquainted with the React Testing library and how to write tests.
Installation
The React Testing library comes by default when you run create-react-app, so it should already be in your project if you created your React project with that command.
Create TypeScript component
First, we will create a component using TypeScript. Make a file called Container.tsx
In the Container component, we will have a div element with a h1 inside of it: