我对TypeScript非常陌生,不知道如何使其工作。
查了一些资料,像是解构(destructuring),但仍然不能正确运行。
import React from "react";
import { StyleSheet, Text, View } from "react-native";
const styles = {
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
};
const App = (style: any): {} => {
const { container } = styles;
return (
<View style={container}>
<Text>Open up App.tsx to start working on your app!</Text>
</View>
);
};
export default App;
下面是错误信息:
(JSX attribute) style?: StyleProp<ViewStyle>
Type '{ flex: number; backgroundColor: string; alignItems: string; justifyContent: string; }' is not assignable to type 'StyleProp<ViewStyle>'.
Type '{ flex: number; backgroundColor: string; alignItems: string; justifyContent: string; }' is not assignable to type 'ViewStyle'.
Types of property 'alignItems' are incompatible.
Type 'string' is not assignable to type 'FlexAlignType'.ts(2322)
index.d.ts(2206, 5): The expected type comes from property 'style' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps> & Readonly<{ children?: ReactNode; }>'