- Published on
Difference between `.tsx` and `.ts` Files
- Authors
- Name
- hwahyeon
.tsx
and .ts
files are both TypeScript files, but the main difference lies in whether JSX (JavaScript XML) syntax is supported.
.ts
Files
1. - Purpose: Used for writing pure TypeScript code
- Features: Supports only TypeScript syntax; JSX is not allowed
- Use Cases: Writing general logic, utility functions, type definitions, and API handling where JSX is not required
.tsx
Files
2. - Purpose: Used for combining TypeScript and JSX syntax
- Features: Supports JSX syntax, mainly used in React components
- Use Cases: Creating React components and working with JSX code Note: To enable JSX, you need to configure the
jsx
option intsconfig.json
:
{
"compilerOptions": {
"jsx": "react-jsx" // Use "react" for React 17 or earlier versions
}
}
Additional Notes
- If JSX is not needed, it is standard practice to use the
.ts
extension. .tsx
files are not limited to React and can be used in other frameworks that support JSX.