This project is a hands-on learning exercise to understand and practice the integration of TypeScript with React. It serves as a foundational boilerplate for building type-safe, scalable web applications.
The primary goal of this project is to explore the core concepts of React while leveraging the power of TypeScript for static typing. This includes:
- Setting up a React development environment with TypeScript.
- Creating functional components with typed props and state.
- Understanding how to handle events in a type-safe manner.
- Structuring a React application for clarity and maintainability.
- Type-Safe Components: All components are built with TypeScript to ensure prop and state types are checked at compile time.
- Modern React: Utilizes modern React features like Hooks (
useState,useEffect, etc.). - Create React App Boilerplate: Bootstrapped with Create React App for a streamlined development experience.
- React
- TypeScript
- Create React App
- HTML5 & CSS3
Follow these instructions to get a copy of the project up and running on your local machine for development and learning purposes.
Make sure you have the following installed on your system:
- Node.js (v16.x or later recommended)
- npm or yarn
-
Clone the repository to your local machine:
git clone https://github.com/Murzuq/React-TypeScript.git
-
Navigate into the project directory:
cd react-typescript -
Install the dependencies:
npm install
or if you use yarn:
yarn install
To start the development server and view the application in your browser:
npm startor
yarn startThis will open the application at http://localhost:3000. The page will automatically reload if you make edits.
The project follows a standard Create React App structure:
react-typescript/
├── public/
│ ├── index.html # The main HTML page
│ └── ...
├── src/
│ ├── components/ # Reusable components (optional, but good practice)
│ ├── App.tsx # The main application component
│ ├── index.tsx # The entry point of the application
│ └── react-app-env.d.ts # TypeScript declarations for CRA
├── package.json
└── tsconfig.json # TypeScript configuration
This project aims to cover the following concepts:
- Component Typing: Defining interfaces for component props (
React.FC<Props>). - State Management: Using the
useStatehook with explicit types. - Event Handling: Typing event handlers for forms, buttons, and other interactive elements.
- Project Configuration: Understanding the roles of
tsconfig.jsonand how it works with a React project.
Throughout this project, I have gained a better understanding of:
-
Typed Component Props: How to define an
interfaceortypefor component props. This ensures that components receive the correct data types, preventing common bugs and improving developer experience with autocompletion.interface GreetingProps { name: string; } const Greeting: React.FC<GreetingProps> = ({ name }) => { return <h1>Hello, {name}</h1>; };
-
State Management with
useState: How to use theuseStatehook with explicit types. TypeScript can often infer the type, but for complex state, providing a type argument (e.g.,useState<User | null>(null)) is crucial for type safety.const [count, setCount] = useState<number>(0);
-
Typed Event Handling: How to correctly type event handlers for DOM events. This prevents errors when accessing event properties and provides clarity on what kind of event is being handled.
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { // `event` is fully typed };