销毁道具时打字稿无法识别类型

时间:2018-10-02 19:18:46

标签: javascript reactjs typescript

我有一个将道具分解为的组件:

<SupportComment {...props.supportComments} />

具有以下几种类型:

export type UserReview = {
  ....
  supportComments: SupportComment[];
  ....
};

export type SupportComment = {
  comment_text: string;
  department: string;
  name: string;
  submitted_at: number;
};

和我的组件:

type Props = SupportComment;

const SupportComment: React.SFC<Props> = ({ comment_text: commentText, department }) => (

但是我似乎不断收到某种错误:

Type '{ [n: number]: SupportComment; length: number; toString(): string; toLocaleString(): string; pop(): SupportComment; push(...items: SupportComment[]): number; concat(...items: ConcatArray<SupportComment>[]): SupportComment[]; concat(...items: (SupportComment | ConcatArray<...>)[]): SupportComment[]; ... 26 more ...; ...' is not assignable to type 'SupportComment'.
  Property 'comment_text' is missing in type '{ [n: number]: SupportComment; length: number; toString(): string; toLocaleString(): string; pop(): SupportComment; push(...items: SupportComment[]): number; concat(...items: ConcatArray<SupportComment>[]): SupportComment[]; concat(...items: (SupportComment | ConcatArray<...>)[]): SupportComment[]; ... 26 more ...; ...'.

我已经尝试将道具类型更改为这样:

type Props = SupportComment[];

但这给了我另一个错误:

Type 'SupportComment[] & { children?: ReactNode; }' has no property 'comment_text' and no string index signature.

type Props = SupportComment[] & SupportComment;

但这似乎使我回到第一个错误。

我不确定如何解决此问题,因为该值是一个由1个对象组成的数组,我用.reduce将该对象作为对象返回:

supportComments: data.supportComments.reduce((prev: SupportComment, next: SupportComment) => next, {}),

我在哪里错了?

0 个答案:

没有答案
相关问题