简体   繁体   English

Typescript React-native 样式组件中的类型问题

[英]Type issue in Typescript React-native styled-component

I am trying to create a TouchableOpacity using styled-component in react-native and was working successfully but since i am using typescript its showing some error when using style flowDirection: 'row'我正在尝试使用 react-native 中的样式组件创建 TouchableOpacity 并且工作成功,但是由于我使用的是 typescript,因此在使用样式flowDirection: 'row'

here is my code这是我的代码

interface IDefaultStyle {
  style?: object
}

const RootContainer = styled.TouchableOpacity((props: IDefaultStyle) => ({
  flexDirection: 'row',
  ...props.style
}))

And the type error is类型错误是

Argument of type '(props: IDefaultStyle) => { flexDirection: string; } | { flexDirection: string; }' is not assignable to parameter of type 'TemplateStringsArray'.

类型错误

And when i remove flexDirection and add any other style type.当我删除flexDirection并添加任何其他样式类型时。 the error is gone.错误消失了。

The same issue is seen in styled-components Text when fontWeight: 'bold' is given当给定fontWeight: 'bold'时,在 styled-components Text中也会出现同样的问题

My package.json我的package.json

{
  "name": "@appmaker/ui",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "peerDependencies": {
    "react": "16.9.0",
    "react-native": "0.61.2"
  },
  "devDependencies": {
    "@babel/core": "7.6.4",
    "@babel/preset-typescript": "^7.7.2",
    "@babel/runtime": "7.6.3",
    "@react-native-community/eslint-config": "0.0.3",
    "@types/jest": "^24.0.20",
    "@types/react": "^16.9.11",
    "@types/react-native": "^0.60.22",
    "@types/react-test-renderer": "^16.9.1",
    "@types/styled-components": "^4.1.20",
    "@typescript-eslint/eslint-plugin": "^2.5.0",
    "@typescript-eslint/parser": "^2.5.0",
    "babel-jest": "24.9.0",
    "babel-plugin-styled-components": "^1.10.6",
    "jest": "24.9.0",
    "metro-react-native-babel-preset": "0.51.1",
    "prettier": "^1.18.2",
    "react-test-renderer": "16.9.0",
    "tslint": "^5.20.0",
    "tslint-config-prettier": "^1.18.0",
    "tslint-config-standard": "^8.0.1",
    "tslint-eslint-rules": "^5.4.0",
    "tslint-react": "^4.1.0",
    "typescript": "^3.6.3",
    "typescript-plugin-styled-components": "^1.4.3"
  },
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  },
  "bit": {
    "env": {
      "compiler": "bit.envs/compilers/react-typescript@3.0.27"
    },
    "componentsDefaultDirectory": "components/{name}",
    "packageManager": "npm"
  },
  "dependencies": {
    "i": "^0.3.6",
    "npm": "^6.12.0",
    "prop-types": "^15.7.2",
    "react-native-image-slider": "^2.0.3",
    "react-native-image-slider-box": "^1.0.5",
    "react-native-snap-carousel": "^3.8.3",
    "styled-components": "^4.4.0"
  }
}

it will be greatly appreciated if someone could help me with this如果有人可以帮助我,将不胜感激

Edit component usage编辑组件使用

/**
 * ## AuthorView  | SellerView
 * Component to show Author info
 * @param props
 */
const AuthorView = (props: IProps) => {

  const { name, style, imageUrl, rating, subtext, onPress } = props

  // if imageUrl is not provided use default star icon
  const imageSource = imageUrl ? { uri: imageUrl } : require('../../icons/star-grey.png')

  return(
    <RootContainer onPress={onPress} style={style}>
        <ImageViewContainer>
            <ImageView
              defaultImage={imageUrl ? false : true}
              source={ imageSource }
            />
        </ImageViewContainer>
        <RightContainer>
            <Name>{name}</Name>
            <View style={{ flexDirection: 'row' }}>
            {rating && <RatingView style={{ marginRight: 3 }} rating={rating} />}
            <SubText>{subtext}</SubText>
            </View>
        </RightContainer>
    </RootContainer>
  )
}

export default AuthorView

flexDirection: 'row' as const flexDirection:'行'作为常量

This solved the issue for me.这为我解决了这个问题。

change改变

const RootContainer = styled.TouchableOpacity((props: IDefaultStyle)
=> ({   flexDirection: 'row',   ...props.style }))

to

const RootContainer = styled.TouchableOpacity<IDefaultStyle>`  
    flexDirection: row;
    ${...props.style}
`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有TypeScript的React-Native中的样式组件键入无状态功能组件? - How to type Stateless Functional Component using styled-component in React-Native with TypeScript? React-native:样式组件和 typescript 中的多个样式道具 - React-native: multiple style props in styled-component and typescript Typescript 样式组件按钮中的问题 ( React-Native ) - Typescript issue in Styled component Button ( React-Native ) 使用 Typescript 响应样式组件,类型错误 - React styled-component with Typescript, types error 样式化组件的“css”道具和 Storybook 的 TypeScript 问题 - TypeScript issue with styled-component's "css" prop and Storybook React typescript 将值从滑块传递到样式组件 - React typescript pass value from slider to styled-component 如何使用 react、typescript 和 styled-component 将 onclick 方法从父组件传递给子组件? - How to pass onclick method to child component from parent using react, typescript, and styled-component? React:将 CSSProperties 转换为样式化组件 - React: Convert CSSProperties to Styled-Component Typescript 样式组件错误:“类型 '{ children: string; }' 没有与类型 'IntrinsicAttributes' 相同的属性。” - Typescript styled-component error: "Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes'." React 样式组件作为属性 TypeScript 问题 - React styled component as a property TypeScript issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM