【发布时间】:2018-04-15 02:09:39
【问题描述】:
我正在尝试将 TypeScript 集成到我们的项目中,到目前为止,我偶然发现了 styled-components 库的一个问题。
考虑这个组件
import * as React from "react";
import styled from "styled-components/native";
import { TouchableOpacity } from "react-native";
// -- types ----------------------------------------------------------------- //
export interface Props {
onPress: any;
src: any;
width: string;
height: string;
}
// -- styling --------------------------------------------------------------- //
const Icon = styled.Image`
width: ${(p: Props) => p.width};
height: ${(p: Props) => p.height};
`;
class TouchableIcon extends React.Component<Props> {
// -- default props ------------------------------------------------------- //
static defaultProps: Partial<Props> = {
src: null,
width: "20px",
height: "20px"
};
// -- render -------------------------------------------------------------- //
render() {
const { onPress, src, width, height } = this.props;
return (
<TouchableOpacity onPress={onPress}>
<Icon source={src} width={width} height={height} />
</TouchableOpacity>
);
}
}
export default TouchableIcon;
下面一行抛出 3 个错误,本质上是相同的 <Icon source={src} width={width} height={height} />
输入{来源:任何;宽度:字符串;高度:字符串;} 不可分配 键入 IntrinsicAttributes ...类型中缺少属性“onPress” {来源:任何;宽度:字符串;高度:字符串;}
不完全确定这是什么以及如何解决它,我是否需要在 Icon 或类似的东西上声明这些?
编辑: 打字稿v2.6.1,样式组件v2.2.3
【问题讨论】:
标签: javascript reactjs typescript jsx styled-components