样式定义中具有fontweight css规则的组件会导致打字错误:没有重载与此定义错误匹配

时间:2020-04-16 19:09:04

标签: reactjs typescript react-native react-sketchapp

我正在使用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;
  ...

这里有几个问题:

  1. 我看到的错误是否表示类型定义中的问题 或者是其他东西?
  2. 该如何解决?

0 个答案:

没有答案