jii*_*Cho 5 typescript react-native
我很难理解为什么当我使用“react-native”中的组件时打字稿会给我这个错误<ImageBackground>
消息<Image>
。
错误信息:
没有重载与此调用匹配。重载第 1 个(共 2 个)“(props: ImageBackgroundProps | Readonly): ImageBackground”出现以下错误。类型 '{ 子元素:元素;样式:{ 弹性:数字;justifyContent:“flex-end”;}; resizeMode:“覆盖”;来源:任何;}' 不可分配给类型“IntrinsicAttributes & IntrinsicClassAttributes & Readonly”。类型“IntrinsicAttributes & IntrinsicClassAttributes & Readonly”上不存在属性“children”。重载 2 个,共 2 个,“(props: ImageBackgroundProps, context: any): ImageBackground”,出现以下错误。类型 '{ 子元素:元素;样式:{ 弹性:数字;justifyContent:“flex-end”;}; resizeMode: "覆盖"; 来源:任何;}' 不可分配给类型“IntrinsicAttributes & IntrinsicClassAttributes & Readonly”。类型“IntrinsicAttributes & IntrinsicClassAttributes & Readonly”上不存在属性“children”。
主要源代码:
import React from "react";
import {
ImageBackground,
StyleSheet,
View,
} from "react-native";
export default function WelcomeScreen() {
return (
<ImageBackground
style={styles.background}
resizeMode="cover"
source={require("../assets/images/background.jpg")}
>
<View>
<View style={styles.logginButton}></View>
<View style={styles.registerButton}></View>
</View>
</ImageBackground>
);
}
const styles = StyleSheet.create({
background: {
flex: 1,
justifyContent: "flex-end",
},
logginButton: {
width: "100%",
height: 70,
backgroundColor: "#fc5c65",
},
registerButton: {
width: "100%",
height: 70,
backgroundColor: "#4ecdc4",
},
});
Run Code Online (Sandbox Code Playgroud)
因为错误消息听起来像是我无法在 ImageBackground 组件中传递子元素,所以当我将 ImageBackground 组件更改为自关闭元素时(就像<ImageBackground source={image source} />
错误消息消失一样)。
我当前使用的另一个解决方案是定义引用 expo typescript 模板的自定义组件。在 Themed.tsx 中,模板定义了自定义<Text>
和<View>
组件来应用自定义主题。
该代码当前有效:
import React from "react";
import {
ImageBackground as DefaultImageBackground,
StyleSheet,
View,
} from "react-native";
type ImageBackgroundProps = DefaultImageBackground["props"] & {
children: React.ReactNode;
};
function MyBackground(props: ImageBackgroundProps) {
return <DefaultImageBackground {...props} />;
}
export default function WelcomeScreen() {
return (
<MyBackground
style={styles.background}
resizeMode="cover"
source={require("../assets/images/background.jpg")}
>
<View>
<View style={styles.logginButton}></View>
<View style={styles.registerButton}></View>
</View>
</MyBackground>
);
}
const styles = StyleSheet.create({
background: {
flex: 1,
justifyContent: "flex-end",
},
logginButton: {
width: "100%",
height: 70,
backgroundColor: "#fc5c65",
},
registerButton: {
width: "100%",
height: 70,
backgroundColor: "#4ecdc4",
},
});
Run Code Online (Sandbox Code Playgroud)
但我认为我的解决方案没有意义,ImageBackground 组件应该能够采用子元素。我的主要源代码中是否存在语法错误?
我也有这个问题。我通过更改我正在使用的节点版本来修复它。此问题一直存在于节点16.14.0
上16.15.0
。我注意到:
16.14.2
引入npm8.5.0
16.15.0
引入npm8.5.5
16.3.2
引入npm8.1.2
17.0.0
引入npm8.1.0
我们只是保留该项目16.13.2
而不是更新。
我目前的理论是,npm 中的这些差异导致了 tsc 解释规则的差异。我很想知道我是否错了。
查看 ImageBackground 的源代码我发现
class ImageBackground extends React.Component<$FlowFixMeProps> {
setNativeProps(props: Object) {
// Work-around flow
const viewRef = this._viewRef;
if (viewRef) {
viewRef.setNativeProps(props);
}
}
Run Code Online (Sandbox Code Playgroud)
其中 <$FlowFixMeProps> 设置为:
type $FlowFixMeProps = /*unresolved*/ any
Run Code Online (Sandbox Code Playgroud)
有没有可能tsc不喜欢任何一个?
归档时间: |
|
查看次数: |
3942 次 |
最近记录: |