Home > Mobile >  No overload matches this call. Overload 1 of 2, '(...items: ConcatArray<never>[]): never[
No overload matches this call. Overload 1 of 2, '(...items: ConcatArray<never>[]): never[

Time:08-15

Making recursive comment structure, I use the following function.

 interface IComment {
      _id: string;
      body: string;
      forum_id: string;
      parent_comment_id: string;
      author_name: string;
      author_id: string;
      authorPic: string;
      votes: number;
      h_voted: number;
      created_at: string;
      updated_at: string;
    }
    
    const getCommentsWithChildren = (comments: IComment[]) => {
      
      const commentsWithChildren = comments.map((comment) => ({
        ...comment,
        children: [],// This is new empty child array
      }));
      commentsWithChildren.forEach((childComment) => {
        const { parent_comment_id } = childComment;
        if (parent_comment_id) {
          const parent = commentsWithChildren.find(
            (comment) => parent_comment_id === comment._id
          );
          if (parent !== undefined) {
            parent.children = parent.children.concat(childComment);// This part make the error
          }
        }
      });
      return commentsWithChildren.filter(
        ({ parent_comment_id, body, children }) =>
          parent_comment_id === null && (body !== null || children.length > 0)
      );
    };

As you can see, to make a child object array, add 'children: []'

and then in recursive function add child by

'parent.children = parent.children.concat(childComment);'

above line cause the error

'No overload matches this call. Overload 1 of 2, '(...items: ConcatArray[]): never[]', gave the following error.

Argument of type '{ children: never[]; _id: string; body: string; forum_id: string; parent_comment_id: string; author_name: string; author_id: string; authorPic: string; votes: number; h_voted: number; created_at: string; updated_at: string; }' is not assignable to parameter of type 'ConcatArray'.

Type '{ children: never[]; _id: string; body: string; forum_id: string; parent_comment_id: string; author_name: string; author_id: string; authorPic: string; votes: number; h_voted: number; created_at: string; updated_at: string; }' is missing the following properties from type 'ConcatArray': length, join, slice

Overload 2 of 2, '(...items: ConcatArray[]): never[]', gave the following error.

Argument of type '{ children: never[]; _id: string; body: string; forum_id: string; parent_comment_id: string; author_name: string; author_id: string; authorPic: string; votes: number; h_voted: number; created_at: string; updated_at: string; }' is not assignable to parameter of type 'ConcatArray'.'

maybe 'children: []' is never type...

In typescript, how to solve this problem? thanks very much for reading this writing.

CodePudding user response:

When you create your empty array within commentsWithChildren you create an array of never[] - which is what cannot be concatenated.

Intellisense showing that the type is set to never[]

You should explicitly set the type of commentsWithChildren or the array you set to commentsWithChildren.children.

CodePudding user response:

Thank your N.J.Dawson!!

I solved the above error.

The following code is that.

interface IComment {
  _id: string;
  body: string;
  forum_id: string;
  parent_comment_id: string;
  author_name: string;
  author_id: string;
  authorPic: string;
  votes: number;
  h_voted: number;
  created_at: string;
  updated_at: string;
}

interface ICommentWithChildren extends IComment {
  children: IComment[];
}

const getCommentsWithChildren = (comments: IComment[]) => {
  const commentsWithChildren: ICommentWithChildren[] = comments.map(
    (comment) => ({
      ...comment,
      children: [],
    })
  );
  commentsWithChildren.forEach((childComment) => {
    const { parent_comment_id } = childComment;
    if (parent_comment_id) {
      const parent = commentsWithChildren.find(
        (comment) => parent_comment_id === comment._id
      );
      if (parent !== undefined) {
        parent.children = parent.children.concat(childComment);
      }
    }
  });
  return commentsWithChildren.filter(
    ({ parent_comment_id, body, children }) =>
      parent_comment_id === null && (body !== null || children.length > 0)
  );
};

added part is

interface ICommentWithChildren extends IComment {
  children: IComment[];
}

and modified part is

const commentsWithChildren: ICommentWithChildren[]

thanks again!!

  • Related