要配置我的本地响应项目,我会严格按照以下步骤操作:https://facebook.github.io/react-native/blog/2018/05/07/using-typescript-with-react-native。 但这是我要编译的,我从打字稿编译器中得到了十五个错误。
这些是一些错误:
信息:
"@types/react": "^16.7.3",
"@types/react-native": "^0.57.8",
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "0.49.1",
"react-native-typescript-transformer": "^1.2.10",
"react-test-renderer": "16.6.0-alpha.8af6728",
"typescript": "^3.1.6"
UPDATE
tsconfig.json
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"allowJs": true,
"jsx": "react-native",
"sourceMap": true,
"outDir": "./build",
"rootDir": "./src",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"exclude": ["build", "index.js", "node_modules"]
}
package.json
{
"name": "test",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"tsc": "tsc --watch"
},
"dependencies": {
"react": "16.6.0-alpha.8af6728",
"react-native": "^0.57.1"
},
"devDependencies": {
"@types/react": "^16.7.3",
"@types/react-native": "^0.57.8",
"babel-jest": "23.6.0",
"jest": "23.6.0",
"metro-react-native-babel-preset": "0.49.1",
"react-native-typescript-transformer": "^1.2.10",
"react-test-renderer": "16.6.0-alpha.8af6728",
"typescript": "^3.1.6"
},
"jest": {
"preset": "react-native"
}
}
App.tsx
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
rn-cli.config.js
module.exports = {
getTransformModulePath() {
return require.resolve('react-native-typescript-transformer');
},
getSourceExts() {
return ['ts', 'tsx'];
},
};
其他文件是由react-native自动生成的文件。
答案 0 :(得分:2)
React Native类型声明似乎与TypeScript默认加载的浏览器DOM声明冲突。要停止加载DOM声明,请将lib
中的["es2017"]
选项设置为target
(与您的tsconfig.json
匹配)。 (lib
的默认值将包括es2017
和dom
。)有一个similar question(尽管掩盖了设置lib
选项的正确答案)在那里)和this issue report中的其他讨论。
答案 1 :(得分:1)
对于那些试图使用打字稿配置react-native的人:
检查我们的boilerplate。此存储库包含所有初始配置,以使它们可以与新的expo-31,babel 7,打字稿和笑话一起工作。