我正在尝试将 TypeScript 集成到我们的项目中,到目前为止,我偶然发现了 风格化组件库的一个问题。
考虑这个组件
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个错误,它们在本质上是相同的
类型{ source: any; width: string; height: string; }不可赋值 类型中缺少“ onPress”属性 { source: any; width: string; height: string; }
不完全确定这是什么以及如何修复它,我是否需要在 Icon
或类似的东西上声明这些?
编辑: 打字稿 v2.6.1
,样式化组件 v2.2.3