能否不使用TypeScript基于Expo开发React Native应用?求TSX转JS步骤
Expo项目中将TSX文件转换为JS文件的详细步骤
1. 重命名TSX文件
把项目中所有.tsx后缀的文件改为.jsx(保留JSX语法时使用)或.js(纯JS逻辑文件使用)。比如将App.tsx重命名为App.jsx,components/Button.tsx改为components/Button.jsx。
2. 移除TypeScript专属语法
逐个修改文件内容,删除所有TS类型相关代码:
- 删掉变量、函数的类型注解:
const age: number = 25→const age = 25 - 移除函数参数的类型声明:
const handleClick = (e: React.MouseEvent) => {}→const handleClick = (e) => {} - 删除接口、类型别名:直接移除
interface User { name: string }这类代码 - 去掉泛型、类型断言:
const items = response.data as Item[]→const items = response.data - 替换枚举(enum):将
enum Theme { Light, Dark }改为普通对象const Theme = { Light: 'Light', Dark: 'Dark' }
3. 清理TS相关配置与依赖
- 删除项目根目录下的
tsconfig.json文件(如果存在) - 卸载不需要的TypeScript依赖,在终端执行:
npm uninstall typescript @types/react @types/react-native # 或使用yarn yarn remove typescript @types/react @types/react-native - 检查
expo.config.js或app.json,确保没有typescript: true这类强制启用TS的配置,如有则改为false或直接删除该字段
4. 测试并修复剩余问题
运行npx expo start启动项目,根据终端报错逐个修复残留的TS语法问题,比如未清理的类型标注、TS特有的工具函数调用等。
内容的提问来源于stack exchange,提问作者Fikri Achmad Anshori
相关产品推荐
相关产品推荐

