If you’ve started learning React, you’ve probably seen files ending with .jsx or .tsx and wondered — what’s the difference?
You may have also heard that many developers now prefer using TypeScript with React.
So what is TypeScript, and where does it fit in the world of React, JSX, and the Virtual DOM? Let’s make it simple.
💡 What Is TypeScript?
TypeScript is a programming language built on top of JavaScript.
You can think of it as JavaScript with extra safety rules.
It helps developers catch mistakes before the code runs, by checking that the right kinds of data are being used in the right places.
For example, if you tell TypeScript that something should be a number, it won’t let you accidentally treat it like text.
let age: number = 25;
age = "twenty-five"; // ❌ Error: Type 'string' is not assignable to type 'number'
This kind of type checking makes your code safer and easier to maintain — especially as your React app grows larger.
⚙️ How TypeScript Works with React
React already lets you describe how your app looks using JSX — an HTML-like syntax inside JavaScript.
When you combine TypeScript and JSX, you use the .tsx file extension, which stands for TypeScript + JSX.
So, instead of writing your React code in .jsx files, you write it in .tsx files like this:
type WelcomeProps = {
name: string;
};
function Welcome({ name }: WelcomeProps) {
return <h1>Hello, {name}!</h1>;
}
Here’s what’s happening:
WelcomePropsdefines the shape of the data (props) this component expects.- TypeScript checks that
nameis always a string. - If you try to use it incorrectly, TypeScript warns you before you even run the app.
<Welcome name={123} /> // ❌ Error: Type 'number' is not assignable to type 'string'
🧩 Where TypeScript Fits in the React Flow
Let’s see how everything fits together inside the React ecosystem:
| Step | Role | Example |
|---|---|---|
| TypeScript | Checks your code for errors and type safety before running it | Makes sure props and variables match expected types |
| JSX | Describes what your user interface should look like | <h1>Hello, {name}</h1> |
| React (Virtual DOM) | Efficiently updates the actual web page when data changes | Renders only what changed |
So, TypeScript helps developers catch errors early, JSX makes the code easier to read and write, and the Virtual DOM makes the app fast and efficient. They work together — not as separate tools, but as parts of a modern, efficient development workflow.
🚀 Why Developers Use TypeScript in React
Here’s why TypeScript has become almost a standard in modern React projects:
- ✅ Fewer bugs – You catch errors early, before your app runs.
- 📘 Clearer components – You know exactly what props and data types a component expects.
- 💡 Smarter autocomplete – Editors like VS Code suggest correct properties automatically.
- 🧭 Easier to refactor – You can safely rename, move, or reorganize code without breaking things.
- 👥 Better collaboration – Other developers instantly understand how your components work.
These benefits make TypeScript a huge time-saver — especially for medium to large projects.
🔍 Real Example: Props With and Without TypeScript
Let’s look at the same React component written first in plain JavaScript, and then in TypeScript, so you can see exactly what TypeScript adds.
🟢 Without TypeScript (Plain React)
function Button({ label, disabled }) {
return <button disabled={disabled}>{label}</button>;
}
// ✅ Works fine
<Button label="Click Me" disabled={true} />
// ⚠️ Also runs — but not safe!
<Button label="Click Me" disabled={1} />
In plain JavaScript, React will not warn you about the disabled={1} mistake.
Your app still runs, but you’ve passed a number where a boolean was expected — and it could cause unexpected behavior.
🔵 With TypeScript (Safer React)
type ButtonProps = {
label: string;
disabled?: boolean; // optional prop
};
function Button({ label, disabled = false }: ButtonProps) {
return <button disabled={disabled}>{label}</button>;
}
// ✅ Works fine
<Button label="Click Me" disabled={true} />
// ❌ Error: Type 'number' is not assignable to type 'boolean'
<Button label="Click Me" disabled={1} />
In TypeScript:
- You define what props should look like using the
ButtonPropstype. - If you pass the wrong type of data (like a number instead of a boolean),
TypeScript catches the error before the code runs.
🧠 The Key Takeaway
| Feature | JavaScript | TypeScript |
|---|---|---|
| Type Checking | ❌ None — mistakes go unnoticed | ✅ Errors caught before running |
| Editor Suggestions | ⚙️ Limited | 💡 Smart autocomplete for props and variables |
| Reliability | ⚠️ Depends on runtime | 🧾 Safer and more predictable |
In short:
TypeScript helps you avoid them before they cause bugs.
JavaScript will let you make mistakes silently.
🌍 In the Bigger Picture
TypeScript doesn’t change how React works internally — it just makes you more confident while writing React code.
- JSX still defines what your UI looks like.
- The Virtual DOM still ensures your UI updates efficiently.
- TypeScript simply ensures your code is safe and predictable before it ever reaches the browser.
Together, they form the modern React stack — powerful, efficient, and beginner-friendly once you understand the flow.
🧭 In a Nutshell
- TypeScript = Adds safety and catches bugs before runtime.
- JSX = Lets you describe your UI visually.
- Virtual DOM = Keeps React fast by updating only what changes.
When used together, they make React development smoother, safer, and more enjoyable — whether you’re building a small app or a production-scale project.