RN中如何使用TypeScript

1. 将 TypeScript 以及 React Native 和 Jest 的依赖添加到您的项目中。

  yarn add --dev typescript @types/jest @types/react @types/react-native @types/react-test-renderer
  # or for npm
  npm install --save-dev typescript @types/jest @types/react @types/react-native @types/react-test-renderer

2. 添加一个 TypeScript 配置文件。在项目的根目录中创建一个tsconfig.json

{
        "compilerOptions": {
          "allowJs": true,
          "allowSyntheticDefaultImports": true,
          "esModuleInterop": true,
          "isolatedModules": true,
          "jsx": "react-native",
          "lib": ["es2017"],
          "moduleResolution": "node",
          "noEmit": true,
          "strict": true,
          "target": "esnext"
        },
        "exclude": [
          "node_modules",
          "babel.config.js",
          "metro.config.js",
          "jest.config.js"
        ]
      }

3. 创建一个jest.config.js文件来配置 Jest 以使用 TypeScript:

    module.exports = {
        preset: 'react-native',
        moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
      };

4. 创建以.ts或.tsx为扩展名的文件。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容