我正在使用basic-setup-typescript示例来设置react-sketchapp。
由于与样式相关的类型错误,示例应用程序未编译。
看起来像是样式定义中带有fontweight
css规则的Text和View组件会导致打字错误:No overload matches this definition error
考虑来自my-command.tsx的代码段
const Swatch = ({ name, hex }: SwatchProps) => (
<View
name={`Swatch ${name}`}
style={{
height: 96,
width: 96,
margin: 4,
backgroundColor: hex,
padding: 8,
}}
>
<Text name="Swatch Name" style={{ color: textColor(hex), fontWeight: 'bold' }}>
{name}
</Text>
<Text name="Swatch Hex" style={{ color: textColor(hex) }}>
{hex}
</Text>
</View>
);
您会看到View
组件的样式定义有多个规则,而Text
组件只有两个。此示例导致出现error TS2769: No overload matches this call.
从fontWeight
组件中删除Text
可使代码编译和呈现示例。
<Text name="Swatch Name" style={{ color: textColor(hex) }}>
{name}
</Text>
将fontWeight
移至View
组件会再次导致打字错误。
<View
name={`Swatch ${name}`}
style={{
height: 96,
width: 96,
margin: 4,
backgroundColor: hex,
padding: 8,
fontWeight: 'bold' <--- added
}}
>
看起来类型定义有问题,但是我很难找到它。我确实找到了类型文件(src/types
),其中TextStyle
扩展了ViewStyle
类型定义,并且确实包含fontWeight作为可选内容:
export type TextStyle = ViewStyle & {
...
fontWeight?: string;
...
这里有几个问题: