How to Use ReactJS with TypeScript for Strongly Typed Applications

Introduction

As React applications grow in size and complexity, maintaining code quality and catching bugs early becomes increasingly important. This is where TypeScript comes in. TypeScript is a statically typed superset of JavaScript that enhances development with type safety, auto-completion, and robust tooling.

In this article, we’ll explore how to use ReactJS with TypeScript to build strongly typed, scalable, and maintainable applications. Whether you’re migrating an existing project or starting from scratch, this guide will help you make the most of React and TypeScript together.


Why Use TypeScript with React?

🔹 Type Safety – Catch errors at compile time rather than runtime
🔹 Better Developer Experience – IDE support with autocompletion, refactoring, and tooltips
🔹 Improved Code Readability – Explicit types make code easier to understand
🔹 Scalability – Types make large codebases more maintainable


Getting Started with React and TypeScript

🔧 Create a React + TypeScript App

Use Create React App with the TypeScript template:

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

This sets up your project with .tsx file support and a preconfigured TypeScript environment.


Basic TypeScript Concepts in React

1. Typing Props

type GreetingProps = {
  name: string;
  age?: number; // Optional prop
};

const Greeting: React.FC<GreetingProps> = ({ name, age }) => (
  <h2>Hello {name}! {age && `You're ${age} years old.`}</h2>
);

2. Typing State with useState

const [count, setCount] = useState<number>(0);

For more complex states:

type User = {
  id: number;
  name: string;
};

const [user, setUser] = useState<User | null>(null);

3. Typing Events

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {

console.log(e.target.value);
};

Using Interfaces and Types

You can define props using type or interface.

interface Product {

id: number;
name: string;
price: number;
}

You can also extend types:

interface Item extends Product {

quantity: number;
}

Typing useEffect and API Calls
useEffect(() => {
  const fetchData = async () => {
    const response = await fetch('https://api.example.com/data');
    const result: MyDataType = await response.json();
    setData(result);
  };

  fetchData();
}, []);

Typing Component Refs
const inputRef = useRef<HTMLInputElement>(null);
const focusInput = () => {
  inputRef.current?.focus();
};

Typing Context API
type AuthContextType = {
  user: string;
  login: (name: string) => void;
};

const AuthContext = createContext<AuthContextType | undefined>(undefined);

const AuthProvider: React.FC = ({ children }) => {
  const [user, setUser] = useState('');
  const login = (name: string) => setUser(name);
  return (
    <AuthContext.Provider value={{ user, login }}>
      {children}
    </AuthContext.Provider>
  );
};

Best Practices

✅ Use explicit types for props, state, and events
✅ Prefer interface for public APIs and type for union types or aliases
✅ Create reusable types for consistency across components
✅ Avoid any—it defeats the purpose of TypeScript
✅ Use utility types like Partial, Pick, and Record to transform types
✅ Configure tsconfig.json to enable strict mode for better type checking


Common Utility Types

 

Utility Type Description
Partial<T> Makes all properties optional
Required<T> Makes all properties required
Pick<T, K> Selects a subset of properties
Omit<T, K> Excludes specified properties
Record<K, T> Defines an object type with keys of type K and values of type T

Conclusion

Combining ReactJS with TypeScript gives developers a powerful toolkit for building robust, scalable, and maintainable applications. With strong typing, better tooling, and a smoother development experience, TypeScript helps reduce bugs and improve code quality in modern React projects.

🔑 Key Takeaways:

  • TypeScript improves type safety and developer productivity

  • Use types and interfaces to define clear and consistent component contracts

  • Integrate TypeScript with hooks, props, events, and context for full coverage

  • Follow best practices and use utility types to reduce repetition

By embracing TypeScript in your React apps, you set your projects up for long-term success. ✅

Rakshit Patel

Author Image I am the Founder of Crest Infotech With over 18 years’ experience in web design, web development, mobile apps development and content marketing. I ensure that we deliver quality website to you which is optimized to improve your business, sales and profits. We create websites that rank at the top of Google and can be easily updated by you.

Related Blogs