React Native typescript样式化组件不接受额外的prop

g52tjvyc  于 2023-10-23  发布在  React
关注(0)|答案(1)|浏览(126)

我试图在React Native项目中使用样式化组件,但是当我使用组件时,typescript无法识别我想要传递的样式化的附加属性。
已设置样式的组件

type StyledContainerProps = { color: string }

const StyledTextContainer = styled.View<StyledContainerProps>`
  background-color: ${({ color }) => color};
`;

在返回中使用

<StyledTextContainer color={`${colour.darken(0.4).hex()}CC`}>
   <Text style={[styles.title, styles.whiteText]}>{text}</Text>
</StyledTextContainer>

我在彩色 prop 上得到的 typescript 错误显示

No overload matches this call.

Overload 1 of 2, '(props: ViewProps | Readonly<ViewProps>): View', gave the following error.
    Type '{ children: Element; color: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.
      Property 'color' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.
  Overload 2 of 2, '(props: ViewProps, context: any): View', gave the following error.
    Type '{ children: Element; color: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.
      Property 'color' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.

尽管这类人表现得像

const StyledTextContainer: IStyledComponent<"native", Substitute<ViewProps, StyledContainerProps>> & typeof View

更新:我发现了一个公开问题,说明类似问题here on github

jchrr9hc

jchrr9hc1#

你用过props吗?

const StyledTextContainer = styled.View<StyledContainerProps>`
  background-color: ${props => props.color};
`;

相关问题