Question
I am new to React and working on a project that uses TypeScript. I have noticed both .ts and .tsx file extensions, and I am not sure when each one should be used.
Since both appear in React projects, where should I use .ts and where should I use .tsx?
Short Answer
By the end of this page, you will understand the difference between .ts and .tsx files in TypeScript projects, especially in React. You will learn when to use each extension, why the distinction matters, and how developers typically organize these files in real codebases.
Concept
TypeScript uses different file extensions to tell the compiler what kind of syntax may appear inside a file.
.tsmeans a regular TypeScript file.tsxmeans a TypeScript file that may contain JSX
JSX is the HTML-like syntax commonly used in React, such as:
const element = <h1>Hello</h1>;
That syntax is not plain TypeScript. Because of that, TypeScript needs a special file extension: .tsx.
When to use .ts
Use .ts for files that contain TypeScript code without JSX.
Examples:
- utility functions
- API helpers
- data transformation logic
- type definitions
- custom hooks that do not return JSX
- constants and configuration
Example:
export function add(a: number, b: number): number {
return a + b;
}
When to use .tsx
Mental Model
Think of the file extension as a label on a box.
- A
.tsbox says: This box contains TypeScript only - A
.tsxbox says: This box may contain TypeScript plus React-style markup
If you try to put JSX markup into a .ts box, the compiler gets confused because the label says it should only expect regular TypeScript.
So the extension is not just naming style. It tells the tools how to read the file.
Syntax and Examples
Here is the basic idea.
.ts example
Use .ts for logic-only files:
// math.ts
export function multiply(a: number, b: number): number {
return a * b;
}
This file contains only TypeScript. There is no JSX.
.tsx example
Use .tsx for React components that render JSX:
// Button.tsx
type ButtonProps = {
label: string;
};
export function Button({ label }: ButtonProps) {
return <button>{label}</button>;
}
This file must be .tsx because it returns JSX.
Step by Step Execution
Consider this file:
type Props = {
name: string;
};
export function Greeting({ name }: Props) {
return <h1>Hello, {name}</h1>;
}
Here is what happens step by step:
-
type Props = { name: string; };- A TypeScript type is created.
- It says the component expects one prop called
name, and it must be a string.
-
export function Greeting({ name }: Props)- A function component is declared.
- The parameter is checked against the
Propstype.
-
return <h1>Hello, {name}</h1>;- This returns JSX.
- Because JSX is present, the file must use the
.tsxextension.
-
TypeScript and the React tooling parse the file as TSX.
Real World Use Cases
Here are common real-world cases.
Use .ts for
- API clients
- functions that call
fetchoraxios
- functions that call
- utility functions
- date formatting, math helpers, string cleanup
- type definitions
- shared interfaces and type aliases
- state logic without JSX
- reducers, selectors, validation rules
- configuration files
- route config, environment parsing, constants
- custom hooks without JSX
- hooks that only return values and functions
Example:
export async function getUsers() {
const response = await fetch('/api/users');
return response.json();
}
Use .tsx for
- React components
Real Codebase Usage
In real projects, developers usually keep a clean separation between presentation and logic.
Common pattern
.tsxfiles handle rendering.tsfiles handle logic, data shaping, and reusable helpers
Example structure:
src/
components/
UserCard.tsx
Navbar.tsx
hooks/
useUsers.ts
utils/
formatDate.ts
validateEmail.ts
types/
user.ts
api/
users.ts
Typical usage patterns
Guard clauses in .tsx
Components often use guard clauses before rendering:
type Props = {
isLoading: boolean;
name?: string;
};
export function ProfileHeader({ isLoading, name }: Props) {
if (isLoading) return <p>Loading...</p>;
if (!name) return <p>No user found;
;
}
Common Mistakes
Here are some common beginner mistakes.
1. Using .ts for a component that returns JSX
Broken example:
export function App() {
return <h1>Hello</h1>;
}
Why it fails:
- The file contains JSX.
.tsfiles are not meant to parse JSX.
Fix:
- Rename the file to
.tsx
export function App() {
return <h1>Hello</h1>;
}
2. Assuming all React files must be .tsx
This is not necessary.
Example:
import { useState } ;
() {
[value, setValue] = ();
{ value, setValue };
}
Comparisons
Here is the practical comparison.
| Extension | Meaning | Can contain JSX? | Common use |
|---|---|---|---|
.ts | Regular TypeScript file | No | utilities, types, hooks, API logic |
.tsx | TypeScript + JSX file | Yes | React components and UI files |
.ts vs .tsx
| Question | .ts | .tsx |
|---|---|---|
| Used in TypeScript projects? | Yes |
Cheat Sheet
- Use
.tsfor TypeScript files with no JSX - Use
.tsxfor TypeScript files that contain JSX - React components that return JSX are usually
.tsx - Utility files, type files, API files, and many hooks are usually
.ts
Quick rule
No JSX -> .ts
Has JSX -> .tsx
Examples
export function sum(a: number, b: number) {
return a + b;
}
export function Header() {
return <header>Site Header</header>;
}
Common file types
Button.tsx-> componentUserList.tsx-> component
FAQ
What is .tsx in TypeScript?
.tsx is a TypeScript file extension used for files that contain JSX, which is commonly used in React components.
What is the difference between .ts and .tsx?
.ts is for regular TypeScript code without JSX. .tsx is for TypeScript code that includes JSX.
Should all React files be .tsx?
No. Only files that contain JSX need .tsx. Logic-only files in a React project can stay as .ts.
Can I use hooks in a .ts file?
Yes. If the file uses hooks like useState or useEffect but does not return JSX, .ts is fine.
Can I write a React component in a .ts file?
Only if it does not contain JSX. In practice, most React components return JSX, so they should usually be .tsx.
What happens if I put JSX in a file?
Mini Project
Description
Create a small React TypeScript feature that separates UI code from logic code. This project demonstrates exactly when to use .ts and when to use .tsx by building a product card component that uses a helper function from a separate file.
Goal
Build a React component in .tsx that displays product information and uses a .ts utility file to format the price.
Requirements
Requirement 1 Requirement 2 Requirement 3
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular @ViewChild Returning Undefined: Lifecycle, Child Components, and Fixes
Learn why Angular @ViewChild can be undefined, when it becomes available, and how to access child components correctly using lifecycle hooks.