I am new to react and I am working on a project. I come across both .ts
and .tsx
extensions. I don't understand where should I use .ts
and .tsx
. Any help on this is much appreciated. Thank you!
ts file for TypeScript, there is also a new file extension . tsx . This is the extension that you should use anytime you include the JSX syntax in a React component file. You also need the jsx set to true which is done by default. TypeScript is able to compile JSX directly into JavaScript.
The difference between doing React JS and React TS is the language you write. You write JavaScript or TypeScript, but at the end of the day the bundle is always JavaScript (Note: Deno is different, it natively runs TypeScript, check it out).
. tsx is similar to jsx except it's TypeScript with the JSX language extension.
TypeScript is a language that is a syntactic-superset of JavaScript. JSX is FaceBook's XML-like syntax extension to JavaScript, not a language per se.
Use .ts
for pure TypeScript files.
Use .tsx
for files which contain JSX.
For example, a React component would be .tsx
, but a file containing helper functions would be .ts
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With