этот код ниже является компонентом реагирования, который возвращает таблицу, в которой каждая строка данных извлекается из бэкэнд-API через GraphQL.Данные извлекаются правильно, но я не могу найти правильный интерфейс для данных, извлекаемых компонентом Query
.
Я получаю следующие ошибки:
TypeScript error: Type '({ data: { allTeams }, error, loading }: SectionTableQueryProps) => Element' is not assignable to type '((result: QueryResult<any, OperationVariables>) => ReactNode) | (((result: QueryResult<any, OperationVariables>) => ReactNode) & string) | (((result: QueryResult<any, OperationVariables>) => ReactNode) & number) | ... 4 more ... | (((result: QueryResult<...>) => ReactNode) & ReactPortal)'.
Type '({ data: { allTeams }, error, loading }: SectionTableQueryProps) => Element' is not assignable to type '(result: QueryResult<any, OperationVariables>) => ReactNode'.
Types of parameters '__0' and 'result' are incompatible.
Property 'allTeams' is missing in type 'QueryResult<any, OperationVariables>' but required in type 'SectionTableQueryProps'. TS2322
50 | <tbody>
51 | <Query query={GET_ALL_TEAMS}>
> 52 | {({ data: { allTeams = [] } = {}, error, loading }: SectionTableQueryProps) => {
| ^
53 | if (loading) {
54 | return <div>LOADING</div>
55 | };
TypeScript error: Property 'data' does not exist on type 'SectionTableQueryProps'. TS2339
50 | <tbody>
51 | <Query query={GET_ALL_TEAMS}>
> 52 | {({ data: { allTeams = [] } = {}, error, loading }: SectionTableQueryProps) => {
| ^
53 | if (loading) {
54 | return <div>LOADING</div>
55 | };
import React from 'react';
import gql from 'graphql-tag';
import { Query } from 'react-apollo';
import { ApolloError } from 'apollo-client';
interface Team {
id: string;
name: string;
}
interface Data {
allTeams?: Team[];
}
interface SectionTableQueryProps {
allTeams: Team[];
error?: ApolloError;
loading: boolean;
}
const GET_ALL_TEAMS = gql`
query {
allTeams {
id
name
}
}
`;
const SectionTable = (props) => (
<table className="table table-hover table-responsive">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<Query query={GET_ALL_TEAMS}>
{({ data: { allTeams = [] } = {}, error, loading }: SectionTableQueryProps) => {
if (loading) {
return <div>LOADING</div>
};
if (error !== undefined) {
return <div>ERROR</div>
};
return (
// jsx code of the component
);
}}
</Query>
</tbody>
</table>
)
export default SectionTable;
Как правильно написать свои интерфейсы Typescript?