简体   繁体   English

如何将 TSX 转换为 JSX

[英]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).我有一个项目,我在 Expo 和 TypeScript 中编写了一些新代码(因为我更习惯于使用类型安全的语言)。 However, I want to port what I wrote to something that was written with Expo with JavaScript.但是,我想将我写的东西移植到用 Expo 和 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.我可以手动剥离类型,因为我是 go 来解决问题,但我想知道是否有更自动的方法来做这件事。 I know typescript eventually compiles down to JavaScript but I want to keep the HTML embedded code the same when I do the conversion.我知道 typescript 最终编译为 JavaScript 但我想在进行转换时保持 HTML 嵌入式代码相同。 Is it possible?可能吗?

The answer provided by @Jason is close to what I needed. @Jason 提供的答案接近我所需要的。

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.这会在js文件夹中生成.js.jsx文件,这些文件可以复制到非打字稿系统。

--noEmit false is needed for the Expo generated tsconfig.js which is noEmit: true --noEmit false需要 Expo 生成的 tsconfig.js 是noEmit: true

The -t es2020 generates output using ES2020 standards so you can get import and export along with the async await intact. -t es2020使用 ES2020 标准生成 output ,因此您可以在async await原封不动的情况下进行importexport

In the tsconfig.json file change "jsx": "react" to "jsx": "preserve" and then run tsc.在 tsconfig.json 文件"jsx": "react"更改为"jsx": "preserve" ,然后运行 tsc。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM