Archimedes Trajano
Archimedes Trajano

Reputation: 41750

How do I convert TSX to JSX

I have a project where I wrote some new code in Expo with TypeScript (since I am more used to having a type safe language). However, I want to port what I wrote to something that was written with Expo with JavaScript.

I can manually just strip off the types as I go along to solve the problem, but I was wondering if there's a more automatic way of doing it. I know typescript eventually compiles down to JavaScript but I want to keep the HTML embedded code the same when I do the conversion. Is it possible?

Upvotes: 10

Views: 38323

Answers (2)

Archimedes Trajano
Archimedes Trajano

Reputation: 41750

The answer provided by @Jason is close to what I needed.

In the end I ran this command

npx tsc --jsx preserve -t es2020 --outDir js --noEmit false

This generated the .js and .jsx files in the js folder which can be copied over to non-typescript systems.

--noEmit false is needed for the Expo generated tsconfig.js which is noEmit: true

The -t es2020 generates output using ES2020 standards so you can get import and export along with the async await intact.

Upvotes: 24

Jason
Jason

Reputation: 731

In the tsconfig.json file change "jsx": "react" to "jsx": "preserve" and then run tsc.

https://www.typescriptlang.org/docs/handbook/jsx.html

Upvotes: 4

Related Questions