我刚刚开始使用 Typescript,我正在尝试将它用于一个新的 React Native 项目。但是,我可以让 TS 进行编译。我的设置基于 this article使用 npm @types 除外。我在我的 IDE (Atom) 中获得了正确的类型提示,但是当我尝试编译时,我收到以下错误:
node_modules/@types/react-native/index.d.ts(19,24): error TS2307: Cannot find module 'react'.
node_modules/@types/react-native/index.d.ts(222,28): error TS2304: Cannot find name 'Component'.
node_modules/@types/react-native/index.d.ts(1017,15): error TS2304: Cannot find name 'Ref'.
...
我的包.json
{
"name": "healthymeIngage",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"react": "15.3.2",
"react-native": "0.35.0"
},
"jest": {
"preset": "jest-react-native"
},
"devDependencies": {
"@types/react": "^0.14.41",
"@types/react-native": "^0.29.36",
"babel-jest": "16.0.0",
"babel-preset-react-native": "1.9.0",
"jest": "16.0.2",
"jest-react-native": "16.0.0",
"react-test-renderer": "15.3.2",
"typescript": "^2.0.3"
}
}
我的 tsconfig.json
{
"compilerOptions": {
"module": "es2015",
"target": "es2015",
"jsx": "react",
"outDir": "build",
"rootDir": "src",
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"sourceMap": true
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"node_modules/@types/**/*.ts"
]
}
最佳答案
由于您使用的是 "typescript": "^2.0.3"
,因此报告的错误是 issue .有解决此问题的方法,即使用 "moduleResolution": "node"
。请注意,它不会使经典模块解析适用于之后的包。
@Leone:你的修复 "module": "commonjs"
将无法工作,因为 React Native 附带了 ES2015
、require()
模块将导致 React Native 中的运行时错误。
关于javascript - 带有 React Native 的 Typescript 2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40193631/