Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate a tsx file to a js file for React

I am working on a React Project and have a bunch of tsx files that I want to convert as use as javascript for my project. How should I do that?

like image 877
dracarys Avatar asked Oct 21 '25 13:10

dracarys


2 Answers

I am working on a React Project and have a bunch of tsx files that I want to convert as use as javascript for my project. How should I do that

You can use the typescript compiler e.g.

npx -p typescript tsc somefile.tsx
like image 175
basarat Avatar answered Oct 23 '25 05:10

basarat


While the web is full of guide on how to translate React Javascript into Typescript, I don't found any reverse guide (so strange...). Anyway, I had same problem some time ago and I found useful get one of those guide JS => TS for React and read backwards. For example, I used this guide. When he said "JavaScript looks like that so you can translate this into Typescript in this way..." I did the contrary (and it worked!). Of course you can skip all the dependencies part. Try and let us know.

like image 36
Giovanni Esposito Avatar answered Oct 23 '25 04:10

Giovanni Esposito