RN - 找不到模块供应商/react-native-vector-icons 的声明文件

csn*_*ewb 1 react-native expo

我正在使用 expo 34.0.1 进行本机开发。我正在为该项目使用 TypeScript,并tsc --project . --noEmit在测试脚本中开玩笑地运行。这会导致以下错误:

node_modules/@expo/vector-icons/build/createIconSet.d.ts:2:55 - 错误 TS7016:找不到模块 './vendor/react-native-vector-icons/lib/create-icon 的声明文件-放'。'../node_modules/@expo/vector-icons/build/vendor/react-native-vector-icons/lib/create-icon-set.js' 隐式具有 'any' 类型。

2 从 './vendor/react-native-vector-icons/lib/create-icon-set' 导出 { DEFAULT_ICON_COLOR, DEFAULT_ICON_SIZE }; 发现 1 个错误。

npm 错误!代码 ELIFECYCLE npm ERR!错误号 1 npm 错误号!@tsc-test:tsc --project . --noEmitnpm 错误!退出状态 1 npm ERR!npm 错误!@tsc-test 脚本失败。npm 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。

npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!
/Users/.npm/_logs/2019-08-31T19_25_49_598Z-debug.log

tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "module": "es6",
    "target": "es6",
    "lib": ["es2016", "esnext.asynciterable"],
    "jsx": "react-native",
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "types": ["jest"],
    "moduleResolution": "node",
    "allowJs": false,
    "esModuleInterop": true
  },
  "exclude": ["node_modules"]
}
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "test": "npm run tslint && npm run tsc-test && npm run jest",
    "tslint": "tslint --project .",
    "tsc-test": "tsc --project . --noEmit",
    "jest": "jest"
  },
  "dependencies": {
    "@types/enzyme": "^3.10.3",
    "expo": "^34.0.1",
    "moment": "^2.24.0",
    "react": "16.9.0",
    "react-dom": "^16.9.0",
    "react-moment": "^0.9.2",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "react-native-elements": "^1.1.0",
    "react-native-gesture-handler": "~1.3.0",
    "react-native-reanimated": "~1.1.0",
    "react-native-vector-icons": "^6.6.0",
    "react-native-web": "^0.11.4",
    "react-navigation": "^3.12.1"
  },
  "devDependencies": {
    "@types/expo": "^32.0.13",
    "@types/jest": "^24.0.18",
    "@types/react": "^16.9.2",
    "@types/react-test-renderer": "^16.9.0",
    "enzyme": "^3.10.0",
    "enzyme-adapter-react-16": "^1.14.0",
    "jest": "^24.9.0",
    "jest-expo": "^34.0.1",
    "react-test-renderer": "^16.9.0",
    "ts-jest": "^24.0.2",
    "tslint": "^5.19.0",
    "tslint-config-airbnb": "^5.11.1",
    "typescript": "^3.5.3"
  },
  "private": true,
  "jest": {
    "preset": "jest-expo",
    "transform": {
      "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
      "^.+\\.tsx?$": "ts-jest"
    },
    "testMatch": [
      "**/__tests__/**/*.ts?(x)",
      "**/?(*.)+(spec|test).ts?(x)"
    ],
    "moduleFileExtensions": [
      "js",
      "ts",
      "tsx"
    ],
    "globals": {
      "ts-jest": {
        "tsConfig": {
          "jsx": "react"
        }
      }
    },
    "setupFilesAfterEnv": [
      "./src/setupTests.js"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决这个问题?

Sha*_*hah 16

步骤1:

\n

在 package.json 的“scripts”对象中只需添加:

\n
"postinstall": "npx typesync"\n
Run Code Online (Sandbox Code Playgroud)\n

第2步:

\n

运行yarnnpm install以有效运行“安装后”脚本。\n添加所有缺少的包后,您将获得要添加到项目中的所有新类型的列表

\n

它可能看起来像这样:

\n
 yourAppNameHere \xe2\x80\x94 package.json (4 new typings added, 0 unused typings removed)\n\xe2\x94\x9c\xe2\x94\x80 + @types/babel__core\n\xe2\x94\x9c\xe2\x94\x80 + @types/react-native-vector-icons\n\xe2\x94\x9c\xe2\x94\x80 + @types/react\n
Run Code Online (Sandbox Code Playgroud)\n

步骤3:

\n

您可能会被要求运行npm installyarn,这将安装添加的软件包,然后您就可以开始了!

\n


Ahm*_*ien 5

我可以看到您正在使用 eslint。因此,编辑编译器选项并添加是安全的

"noImplicitAny": false,
Run Code Online (Sandbox Code Playgroud)

这将消除您的错误。并且 eslint 将捕获您代码中的任何隐式 any。

我希望根据我的理解这是正确的:-)

  • 这不是问题的答案,这只是把问题推到了地毯下 (7认同)