扩展类型只能从对象类型创建

Ilj*_*lja 3 typescript ecmascript-6 reactjs react-native spread-syntax

我有一个简单的反应本机组件,使用 ts 指定其自己的样式,但是我也想从组件树上方传递我可能在其上使用的任何样式(即我使用此组件的某个地方)

class RatioImage extends React.Component<Props> {
  render() {
    const { width, ratio, style, ...props } = this.props;
    return (
      <Image
        {...props}
        style={{
          width: deviceWidth * (width / 100),
          height: deviceWidth * (width / 100) * ratio,
          ...style
        }}
      />
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

我目前收到以下错误,...style不知道为什么,因为它应该是一个对象?

[ts] Spread types may only be created from object types.
const style: false | ImageStyle | (number & {
    __registeredStyleBrand: ImageStyle;
}) | RecursiveArray<false | ImageStyle | (number & {
    __registeredStyleBrand: ImageStyle;
}) | null | undefined> | null | undefined
Run Code Online (Sandbox Code Playgroud)

Ant*_*ony 7

您需要使用数组语法来在 React Native 中提供多种样式

<Image style={[{ width: x, height; y}, style]} />
Run Code Online (Sandbox Code Playgroud)

请参阅文档以获取更多信息