样式组件props的流类型不起作用

IT技术 reactjs types flowtype styled-components
2021-05-19 10:33:30

我有这个样式的组件:

type Props = {
  iconAlign: string,
};

const IconWrapper: ComponentType<Props> = styled.View`
  margin: 10px 10px 0px 10px;
  position: absolute;
  ${({ iconAlign }: Props) =>
    iconAlign === 'left' ? 'left: -35px;' : 'right: -35px;'}
`;

并这样称呼它:

<IconWrapper iconAlign="left">

但是 Flow 给了我以下错误:

Cannot call styled.View because property iconAlign is missing in object type [1] in the first argument of array element.

"flow-bin": "0.96.0",
"styled-components": "^4.2.0",
libdef: styled-components_v4.x.x (satisfies styled-components@4.2.0)
1个回答

由于一些原因,目前似乎不支持内置组件上的自定义属性,但 Flow 仍在开发对标记模板文字的支持。https://github.com/flow-typed/flow-typed/pull/2933的讨论对于理解为什么您的示例当前不起作用以及为使其成为可能而采取的步骤非常有用。