Css Properties and TextStyle Types

此生再无相见时 提交于 2021-02-11 12:25:31

问题


I have a textStyle which is a type of TextStyle (it comes from react-native types) or React.CSSProperties. I want to pass this type to style attribute for html span element. style attribute accepts type of React.CSSProperties. My interface and span element is:

export interface IBaseTextStyleType {
    textStyle:  React.CSSProperties | TextStyle
}

<span style={style.textStyle}>
    {this.props.children || this.props.text}
</span>

I got this error in style of span:

Type 'CSSProperties | TextStyle' is not assignable to type 'CSSProperties'.
  Type 'TextStyle' is not assignable to type 'CSSProperties'.
    Types of property 'aspectRatio' are incompatible.
      Type 'number' is not assignable to type 'AspectRatio'.

How can I rid of this error ?


回答1:


You try to define type of style.textStyle like this:

<span style={style.textStyle as React.CSSProperties}>
    {this.props.children || this.props.text}
</span>


来源:https://stackoverflow.com/questions/65932634/css-properties-and-textstyle-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!