/    Sign up×
Bounties /Pin to ProfileBookmark

Can TSX and JSX be used together in a project?

+ 1,000
Copy linkTweet thisAlerts:
Oct 13.2023

Could I use both TSX and JSX in the same project? If a component is created in JSX, will there be any issues when importing it into a TSX page?

to post a answer
Front-endJavaScriptTypeScript

2 Replies

Copy linkTweet thisAlerts:
@GeneJReeOct 13.2023 — Answer is Yes. You’re at liberty to use both JSX and TSX in your very same project without major issues when you import a JSX component into a TSX file. This is so because TypeScript is fully compatible with JavaScript, even its syntax (JSX).
You may, however, need a few configurations of your project to support both, but if you bootstrapped your project using Create React App, you’re good to go! It comes preconfigured to support both TSX and JSX out of the box.
Again, you may want to create your components in either of the two, but remember to import as shown below:

// MyComponent.jsx
import React from 'react';

function MyComponent() {
return This is a JSX component;
}

export default MyComponent;


Now, you should be able to import JSX created in TSX file without any issues. As TypeScript understands your JSX code, it will treat it as JS. Using the import example below, you can use your JSX component in a TSX file:


// MyTSXComponent.tsx
import React from 'react';
import MyComponent from './MyComponent';

function MyTSXComponent() {
return (


This is a TSX component that uses a JSX component.

);
}

export default MyTSXComponent;



Lastly you might need to provide type definition for JSX components created in JSX using “.d.ts” file extension. This provides the type information of the component as shown below


// MyComponent.d.ts
declare module 'MyComponent' {
const MyComponent: React.FC;
export default MyComponent;
}

Davinci•003 auto-replyYes, TSX and JSX can be used together in a project. TSX is essentially JSX with TypeScript, which is a statically typed superset of JavaScript.

In a project, you can have some files with a .jsx extension and others with a .tsx extension. The .jsx files will be regular JavaScript with JSX syntax, while the .tsx files will be TypeScript with JSX syntax.

However, it's important to note that if you're using TypeScript (and therefore TSX), it's generally a good idea to use it throughout your entire project for consistency and to take full advantage of TypeScript's features. Mixing TSX and JSX might lead to confusion and potential type errors that TypeScript is designed to prevent.

Here's an example of how you might use TSX:


import React, { FC } from 'react';

interface Props {
name: string;
}

const MyComponent: FC<Props> = ({ name }) => {
return <h1>Hello, {name}!</h1>;
};

export default MyComponent;


And here's a similar example using JSX:


import React from 'react';

const MyComponent = ({ name }) => {
return <h1>Hello, {name}!</h1>;
};

export default MyComponent;


In the TSX example, we're using TypeScript to define the type of the props that MyComponent expects. In the JSX example, we're not doing any type checking.

Was this helpful?

×

Success!

Help @cosmic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...