Node.js 使用typescript配置react native时出错
要配置我的react本机项目,我每分钟执行以下过程:。 但我要编译的这个,我从typescript编译器中得到了15个错误 以下是一些错误: 无法重新声明块作用域变量“navigator”。 后续属性声明必须具有相同的类型。属性“geolocation”的类型必须为“geolocation”,但此处的类型为“GeolocationStatic” 找不到名称“Map”。 后续变量声明必须具有相同的类型 重复标识符“RequestInfo”。 此处还声明了“FormData”。 无法重新声明块作用域变量“console”。 annot重新声明块作用域变量“navigator”。 资料:Node.js 使用typescript配置react native时出错,node.js,reactjs,typescript,react-native,npm,Node.js,Reactjs,Typescript,React Native,Npm,要配置我的react本机项目,我每分钟执行以下过程:。 但我要编译的这个,我从typescript编译器中得到了15个错误 以下是一些错误: 无法重新声明块作用域变量“navigator”。 后续属性声明必须具有相同的类型。属性“geolocation”的类型必须为“geolocation”,但此处的类型为“GeolocationStatic” 找不到名称“Map”。 后续变量声明必须具有相同的类型 重复标识符“RequestInfo”。 此处还声明了“FormData”。 无法重新声明块作用域
"@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"
更新
tsconfig.json
package.json
App.tsx
其他文件是react-native自动生成的文件。看起来react-native类型声明与TypeScript默认加载的浏览器DOM声明冲突。要停止加载DOM声明,请将lib选项设置为[es2017],以匹配tsconfig.json中的目标。lib的默认值包括es2017和dom。尽管设置lib选项的正确答案隐藏在其中,但在中还有其他讨论。看起来React本机类型声明与TypeScript默认加载的浏览器DOM声明冲突。要停止加载DOM声明,请将lib选项设置为[es2017],以匹配tsconfig.json中的目标。lib的默认值包括es2017和dom。尽管设置lib选项的正确答案已被隐藏在其中,但对于那些试图使用typescript配置react native的人来说,还有一个额外的讨论。:
检查我们的电话号码。此repo包含所有初始配置,以使新的expo-31、babel 7、typescript和jest能够正常工作。对于尝试使用typescript配置react native的用户:
检查我们的电话号码。本报告包含所有初始配置,以使新的expo-31、babel 7、typescript和jest能够正常工作。请提供足够的代码来重现问题,以便我们能够找到原因。您完全正确@Matt McCutchen,我刚刚添加了详细信息请提供足够的代码来重现问题,以便我们能够找到原因。你完全正确@Matt McCutchen,我刚刚添加了详细信息谢谢你解决了问题。你相信将来我会因为打字稿而产生其他冲突吗?还是我可以安静地走?我从来没有在前端使用过typescript,我宁愿没有其他问题。我要说的是,现实地说,在某个时候,您可能会遇到更多与typescript相关的问题,但这些问题可能只需要少量的工作就可以解决。是否重视类型检查和编辑器服务以使用TypeScript是您的决定。我当然愿意,否则我想我不会在这里了。非常感谢你解决了这个问题。你相信将来我会因为打字稿而产生其他冲突吗?还是我可以安静地走?我从来没有在前端使用过typescript,我宁愿没有其他问题。我要说的是,现实地说,在某个时候,您可能会遇到更多与typescript相关的问题,但这些问题可能只需要少量的工作就可以解决。是否重视类型检查和编辑器服务以使用TypeScript是您的决定。我当然知道,否则我就不在这儿了。
{
"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"]
}
{
"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"
}
}
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,
},
});
module.exports = {
getTransformModulePath() {
return require.resolve('react-native-typescript-transformer');
},
getSourceExts() {
return ['ts', 'tsx'];
},
};