У меня есть две модели с именами «UserBook» и «Comment».
Book Model: book_comments - это массив типа Comment
export class UserBook {
id: number;
book_name: string;
book_comments: Array<Comment[]>;
constructor(book) {
this.id = book.id ? book.id : null;
this.book_name = book.name ? book.name : null;
this.book_comments = [];
}
}
Comment Model:
export class Comment {
id: number;
comment: string;
date: string;
constructor(comment) {
this.id = comment.id ? comment.id : null;
this.comment = comment.comment ? comment.comment : null;
this.date = comment.date ? comment.date : null;
}
}
Я хочу создать новый комментарий и вставить его в массив комментариев UserBook
book: UserBook;
postNewComment(comment): any {
return this.http.post(environment.api + '/book/comments', {comment: comment}).pipe(
tap(
(response) => {
this.book.book_comments.unshift(new Comment(response['comment']));
},
(error) => {
console.log(error);
}
)
);
}
Но я вижу эту ошибку.
"Ошибка: аргумент типа Comment не назначается параметрутипа Comment "
Если я изменю тип book_comments на" any ", тогда он работает.
Но я не мог понять, почему он не принимает тип Comment [].Может ли кто-нибудь помочь мне об этом.