5

我很难解决这个 TypeScript 问题。

...message: 'Type '{ show: boolean; children: Element; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<ThemedOuterStyledProps<HTMLProps<HTMLDiv...'.
  Property 'show' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Component<ThemedOuterStyledProps<HTMLProps<HTMLDiv...'.'

我正在使用 React + 样式组件 + TypeScript。如果我有这样的样式组件:

const Component = styled.div`
    opacity: ${props => props.show ? 1 : 0}
`

我的 React 组件看起来像这样:

const ReactComponent = (props: { appLoading: boolean }) => (
  <Component show={appLoading} />
)

我对 TypeScript 很陌生,但我认为我需要在组件上定义 show 道具?

4

1 回答 1

10

一种给show道具类型的方法可能是这样的 -

const Component = styled.div`
    opacity: ${(props: {show: boolean}) => props.show ? 1 : 0}
` 

刚刚将 () 添加到“props: {show: boolean}”

于 2017-05-20T20:04:08.490 回答