将prop传递给样式组件的TypeScript错误

Mic*_*ård 5 typescript reactjs styled-components

我很难解决这个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...'.'
Run Code Online (Sandbox Code Playgroud)

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

const Component = styled.div`
    opacity: ${props => props.show ? 1 : 0}
`
Run Code Online (Sandbox Code Playgroud)

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

const ReactComponent = (props: { appLoading: boolean }) => (
  <Component show={appLoading} />
)
Run Code Online (Sandbox Code Playgroud)

我对TypeScript很陌生,但我认为我需要在Component上定义show prop?

Muk*_*oni 8

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

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

刚添加()到"props:{show:boolean}"