ReactNative 中添加TypesScript支持

踩坑一:

通过ReactNative中文网提供的方法: react-native init MyTSProject --template typescript 创建项目,项目初始化运行就报500,也不知道怎么处理

解决方案一:

根据网上博客的思路:

删除全局安装的react-native-cli
npm uninstall -g react-native-cli

然后使用npx安装:
npx react-native init app --template react-native-template-typescript

耐心等待安装结束后即可进行TypeScript开发

解决方案二:

1:package.json中添加:

"devDependencies": {
    "react-native-typescript-transformer": "^1.2.12",
    "react-test-renderer": "16.8.3",
    "typescript": "^3.5.3"
  },

2:在项目根目录下创建文件:tsconfig.json

{
  "compilerOptions": {
    "module": "es2015",
    "target": "es2015",
    "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"
  ]
}

3:创建文件:rn-cli.config.js

module.exports = {
    getTransformModulePath() {
        return require.resolve('react-native-typescript-transformer');
    },
    getSourceExts() {
        return ['ts', 'tsx'];
    },
};

4:完成以上3个步骤后,运行:yarn install,成功执行该命令后即可进行TypeScript开发

 

 

 

 

 

你可能感兴趣的:(React-Native)