When working with TypeScript, you might come across files with different extensions: .tsx
and .ts
. While both are used for writing TypeScript code, there are specific differences between them, primarily related to the type of content they contain and their intended use within a project.
TypeScript (.ts
) Files:
- These files contain TypeScript code without any JSX (JavaScript XML) syntax.
- They are used for regular TypeScript files that primarily deal with logic, functions, classes and other non-UI related aspects of your application.
- Example:
// example.ts
function add(a: number, b: number): number {
return a + b;
}
TypeScript (.tsx
) Files:
- These files support TypeScript code with JSX syntax, which is an extension of JavaScript syntax used for defining React components in React applications.
- JSX allows you to write XML-like code within your TypeScript files, making it easier to describe the structure of your UI components.
- Example
// example.tsx
import React from 'react';interface MyComponentProps {
name: string;
}
const MyComponent: React.FC<MyComponentProps> = ({ name }) => {
return <div>Hello, {name}!</div>;
};
export default MyComponent;