Typescript 2.0 with React Native
Typescript 2.0 with React Native
我刚刚开始使用Typescript,我正试图在一个新的React Native项目中使用它。然而,我可以得到TS编译。除了使用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'.
...
我package.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"
,所以这个错误是由这个报告的问题引起的。有一种方法可以解决这个问题,就是使用"moduleResolution": "node"
。请注意,它不会使经典的模块解析对之后的包起作用。
@Leone:你的修复"module": "commonjs"
将不工作,因为React Native船与ES2015
, require()
模块将导致运行时错误在React Native
我将tsconfig.json
文件中的模块系统("module": "es2015"
行)更改为:
"module": "commonjs",
相关文章:
- React Native通过Navigator将用户输入数据传递到选项卡栏IOS中的组件
- 使用Jest测试React Native应用程序
- 是否存在React Native“;WEB代码安全防护”;
- React Native Listview留有余地
- 如何在React Native中绘制图形
- 在iOS和Android上使用React native时,我还能使用本机第三方lirbraries吗
- 使用createContainer将Meteor数据获取到React Native中时出现问题
- React Native当前是否支持访问用户'的通讯簿
- React Native-What'It’在任何地方都要留下逗号
- React Native DeviceEventEmitter键盘WillShow停止工作
- 与React Native的Chart.js等效
- 如何使用webview在React Native中使用Highcharts
- 如何在React Native的MapView中设置标记
- React Native NavigatorIOS
- 如何获得React Native TextInput以在提交后保持焦点
- 使用react native检测应用程序终止
- 可以在React Native中制作一个自哈希应用程序
- React Native AlertOS api中可能有多个文本字段
- react native中常见的js模块
- 在React Native+Redux中使用Navigator