Many developers moving from JavaScript to TypeScript have this question:
π€ βCan I use both
.jsand.tsxfiles 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
.jsand.tsxfiles 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 |
| IntelliSense | Limited | Better |
| 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
| Extension | Purpose |
|---|---|
.ts | TypeScript without JSX |
.tsx | TypeScript 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!