Implementing TypeScript With React

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

This article with cover implementing TypeScript with React. We will talk about where to use types, how to implement types with state, prop types and more.
Lets get right into it!
To create a react app with TypeScript, we can use create-react-app with the --typescript flag. This will bootstrap a react application with TypeScript.
Using NPM:
npx create-react-app --typescript example-app
Using Yarn:
yarn create react-app --typescript example-app
Change 'example-app' to the name of your application
Once your application is setup, you will notice all js files have a .ts extension because they are now TypeScript files.
When creating a functional component, we can use the FC type exported by react to give the function a functional component type.
This would look like this:
const App: FC = () => {
return <div>
<h1>App</h1>
</div>
}
We can also define types for props by passing in a type to FC like this:
const App: FC<IProps> = (props) => {
return <div>
<h1>App</h1>
</div>
}
IProps would be a type or interface with the types of all props the component receives
Alternatively, we can define the prop types like we would a regular parameter of a function:
const App: FC = (props: IProps) => {
return <div>
<h1>App</h1>
</div>
}
This would work the exact same way.
When using useState, we can define the type of the state by passing it in <> after the declaration.
This would look like this:
const [name, setName] = useState<string>("Blake");
The specifying the type in this case is unnecessary since it can be directly inferred from the string. It would be necessary to define the type if it may change when the value is changed.
Some other types you may want to know include:
HTMLInputElement - for html inputsReact.ChangeEvent - for input change eventsReact.FormEvent - for form submissionsThere are obviously plenty of types out there related to react as you build applications and implement new features you will likely run into type errors which you can solve by simply searching up the error and finding the type needed for the situation.
In this article, we have covered how to implement TypeScript with React and some common types you will often need. I hope you've learned something useful and can start using TypeScript in your React applications.
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
Github: Blake-K-Yeboah
LinkedIn: Blake Yeboah
You can also show your support by buying me a coffee ๐