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 配置文件。在項目的根目錄中創(chuàng)建一個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. 創(chuàng)建一個jest.config.js文件來配置 Jest 以使用 TypeScript:

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

4. 創(chuàng)建以.ts或.tsx為擴展名的文件。

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容