Published on

Difference between `.tsx` and `.ts` Files

Authors
  • avatar
    Name
    hwahyeon
    Twitter

.tsx and .ts files are both TypeScript files, but the main difference lies in whether JSX (JavaScript XML) syntax is supported.

1. .ts Files

  • 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

2. .tsx Files

  • 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 in tsconfig.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.