我正在尝试使用 Apollo 的 React HOC 来获取数据并将其传递给我的组件,但出现以下错误:
Argument of type 'typeof BrandList' is not assignable to parameter of type
'CompositeComponent<{ data?: QueryProps | undefined; mutate?: MutationFunc<{}> | undefined; }>'.
Type 'typeof BrandList' is not assignable to type 'StatelessComponent<{ data?:
QueryProps | undefined; mutate?: MutationFunc<{}> | undefined; }>'.
Type 'typeof BrandList' provides no match for the signature '(props: { data?:
QueryProps | undefined; mutate?: MutationFunc<{}> | undefined; } & { children?: ReactNode; }, context?: any): ReactElement<any>'.
我的文件看起来像这样:
import * as React from 'react'
import {graphql} from 'react-apollo'
import gql from 'graphql-tag'
const BrandsQuery = gql`
query {
allBrands {
id
name
}
}
`
interface IBrand {
id: string
name: string
}
interface IData {
loading: boolean,
allBrands: IBrand[]
}
interface IProps {
data: IData
}
class BrandList extends React.Component<IProps, void> {
public render () {
const {loading, allBrands} = this.props.data
if (loading) {
return (
<div>Loading data..</div>
)
}
return (
<div>
{allBrands.map((brand) => (
<li>{brand.id} - {brand.name}</li>
))}
</div>
)
}
}
export default graphql(BrandsQuery)(BrandList)
^^^^^^^^^
如果我使用{}
而不是接口,代码会编译,但是我不能在render
函数内部使用任何props。
编辑:
我试图将最后一行重写为
export default graphql<any, IProps>(BrandsQuery)(BrandList)
这消除了错误,但是现在当我尝试将组件包含为
<div>
<BrandList />
</div>
我收到以下错误:
Type '{}' is not assignable to type 'Readonly<IProps>'.
Property 'data' is missing in type '{}'.