【发布时间】:2018-12-21 11:35:00
【问题描述】:
我已经使用 TypeScript 搭建了一个项目,但我的主要 index.js 文件引用了 App.js 而不是 App.tsx。
如何设置我的项目以便使用 TypeScript 文件 (App.tsx) 而不是 App.js?
这是我的项目结构:
这是通过react-native init X --template typescript搭建的
【问题讨论】:
-
TS 是一种编译成 JS 的语言。这就是为什么您需要将 .tsx 文件编译为 .js 并将初始索引文件指向已编译文件的原因。
-
@zvona 如何在我的 App.tsx 中编写代码并让它自动编译为 App.js?目前,我有 App.js 和 App.tsx。
-
我感觉你已经这样做了,但只是为了谨慎起见:例如你有 inputFolder /src/App.tsx 和 outputFolder 的结构/dist/App.js。 index.js 将指向 ./dist/App.js。然后你运行 TypeScript watcher (
tsc --watch),它会在你每次进行更改时编译 tsx。 TS 行为在 tsconfig.json 中配置。