reactjs - 如何将 TSX 源代码转换为 TS 代码?
问题描述
本质上,我想要@babel/plugin-transform-react-jsx提供的行为,但保留所有类型、其他 TS 语言功能、ESNext 语法、注释等(我只是想转换 TypeScript XML 格式的代码使用指定的 JSX-factory 函数(例如React.createElement
// jsx
bring-your-own/等)转换成普通的 TypeScript
// in
let name: any = 'world';
const div = <div>hello {name as string}</div>;
// out (classic runtime)
let name: any = 'world';
const div = React.createElement('div', null, 'hello ', name as string);
// out (automatic runtime)
let name: any = 'world';
const div = _jsxs('div', { children: ['hello ', name as string] });
解决方案
我认为你误解了打字稿使用的概念。我们仅在开发中使用 typescript 来实现基于模式的可靠方法。在运行 build 命令时,您的 typescript 代码会被转换为 javascript 代码,并且所有相关的 typescript 功能都会被删除。所以基本上我们在生产中运行 javascript。
我的理解是,您希望在编译打字稿代码时保留@babel/plugin-transform-react-jsx 提供的行为以及相应的打字稿类型。但是在我看来这是不可能的,因为 typescript 编译器只会将 typescript 代码转换为 js,而丢弃所有类型。
推荐阅读
- php - 使用 PHP,我怎样才能找到 [departs_at] 字段的出现次数?
- android - 打开移动数据时如何从wifi网络访问本地url?
- selenium - 如何从文本中获取文本
使用硒标记 - docker - How to add files in docker container volume on build time
- autodesk-forge - 翻译大文件时如何防止会话耗尽
- android - AIDL 中的嵌套接口
- amazon-web-services - 降级实例类型后的 AWS 访问问题
- ruby - ruby club 2个多维key成1 via date等关键元素
- wildfly - 在 Kie 服务器上被禁止登录
- scala - spark jupyter notebook 不显示 scala 控制台输出