Can We Use Both .js and .tsx Files in a React Project? (Complete Guide)

Many developers moving from JavaScript to TypeScript have this question:

πŸ€” β€œCan I use both .js and .tsx files in the same React project?”

The answer is:

βœ… YES β€” absolutely.

In fact, many real-world React applications use a mix of:

  • .js
  • .jsx
  • .ts
  • .tsx

especially during migration from JavaScript to TypeScript.

In this guide, you’ll learn:

  • What .js and .tsx files are
  • Can they work together
  • Real-world usage
  • Benefits and drawbacks
  • Best practices

πŸ“Œ Understanding File Types

Before understanding mixed usage, let’s quickly understand each file type.


πŸ”Ή What is .js?

.js means:

JavaScript file

Example:

function add(a, b) {
return a + b;
}

πŸ”Ή What is .tsx?

.tsx means:

TypeScript + JSX file

Used when:

  • Writing React components
  • Using TypeScript types

Example:

type Props = {
name: string;
};
function User({ name }: Props) {
return <h1>{name}</h1>;
}

Can They Work Together?

YES βœ…

You can absolutely use:

  • .js
  • .jsx
  • .ts
  • .tsx

inside the same React project.


Real-World Scenario

Many companies:

  • Start project in JavaScript
  • Slowly migrate to TypeScript

So they temporarily have:

src/ β”œβ”€β”€ App.tsx β”œβ”€β”€ Header.jsx β”œβ”€β”€ utils.js β”œβ”€β”€ api.ts └── Dashboard.tsx

πŸ‘‰ This is completely normal.


How Does It Work?

When using TypeScript in React:

πŸ‘‰ TypeScript compiler can understand JavaScript files too.


Important Configuration

In tsconfig.json:

{
"compilerOptions": { "allowJs": true }
}

What Does allowJs Do?

"allowJs": true

πŸ‘‰ Allows TypeScript to compile .js files also.

Without this:

❌ TypeScript may ignore JS files.


Example Project Structure

Example

src/ β”œβ”€β”€ components/ β”‚ β”œβ”€β”€ Button.tsx β”‚ β”œβ”€β”€ Navbar.jsx β”‚ β”œβ”€β”€ utils/ β”‚ β”œβ”€β”€ math.js β”‚ β”œβ”€β”€ api.ts β”‚ └── App.tsx

πŸ‘‰ All these files can work together.


Importing Between JS and TSX


βœ… Import JS into TSX

import add from "./utils/math";

βœ… Import TSX Component

import Button from "./components/Button";

Why Developers Mix .js and .tsx


βœ… 1. Gradual Migration

Big applications cannot migrate instantly.

So teams:

  • Convert slowly
  • File by file

βœ… 2. Legacy Code Support

Old JavaScript files may still work perfectly.


βœ… 3. Faster Development

Some utility files may remain simple .js.


Important Differences

Feature.js.tsx
Type Safety❌ Noβœ… Yes
IntelliSenseLimitedBetter
Compile ChecksβŒβœ…
React JSX Supportβœ…βœ…

Benefits of Using .tsx

βœ… Better Error Detection

TypeScript catches mistakes early.


βœ… Strong Type Safety

type User = { name: string; };

βœ… Better Developer Experience

  • Auto suggestions
  • Safer refactoring
  • Better maintainability

Problems You May Face

❌ 1. Type Errors with JS Files

Sometimes TypeScript cannot understand JS structure properly.


❌ 2. Inconsistent Codebase

Mixing too many styles may confuse developers.


❌ 3. Any-Type Problems

JS files may reduce type safety.


Best Practice (Recommended)

βœ… Recommended Approach

If possible:

πŸ‘‰ Gradually move toward TypeScript


πŸš€ Good Strategy

Step 1

Keep old files in JS.


Step 2

Write new components in TSX.


Step 3

Slowly migrate old files.


Example Migration

Old JS Component

function Button(props) {
return <button>{props.title}</button>;
}

Migrated TSX Component

type Props = { title: string;};
function Button({ title }: Props) {
return <button>{title}</button>;
}

Important Note About .ts vs .tsx

ExtensionPurpose
.tsTypeScript without JSX
.tsxTypeScript with JSX

Interview Question

❓ Can we use .js and .tsx together in React?

πŸ‘‰ Answer:

Yes. React projects can use both JavaScript and TypeScript files together. TypeScript supports gradual migration using allowJs.


Final Summary

βœ” .js and .tsx can work together
βœ” Common in real-world migration projects
βœ” Use allowJs: true in TypeScript config
βœ” TSX provides better type safety
βœ” Gradual migration is best practice


πŸ’‘ Found this helpful? Subscribe for simple React and TypeScript guides with real-world examples. Happy Coding!

Discover more from Learners Store

Subscribe to get the latest posts sent to your email.

Leave a comment