No.1:安装依赖
yarn add -D typescript 或者 npm i -D typescript
No.2:在根目录添加 tsconfig.json 文件
tsc --init
No.3:替换 tsconfig.json 文件的内容,内容如下:
{ "compilerOptions": { "module": "es2015", "target": "es6", "moduleResolution": "node", "jsx": "react-native", "noImplicitAny": false, "experimentalDecorators": true, "preserveConstEnums": true, "sourceMap": true, "watch": false, "resolveJsonModule": true, "allowSyntheticDefaultImports": true, "lib": [ "es2015.promise", "es2015", "es2016", "dom" ] }, "filesGlob": [ "app/**/*.ts", "app/**/*.tsx" ], "exclude": [ "index.android.js", "index.ios.js", "build", "node_modules" ]}
No.4:添加相对应的 react 库到 typescript,不然 tsx 代码中不能使用相关模块
yarn add -D @types/react @types/react-native
至此,这个项目已经可以配置使用TypeScript进行ReactNative开发,祝你开发愉快!