【发布时间】:2020-05-18 19:14:25
【问题描述】:
我查看了这个post,但我仍然不明白问题出在哪里。为什么我不能将此数组传递给更新调用?
// create object with new post props
const newPost = await this.postRepository.create(data);
await this.postRepository.save(newPost);
// push postid into posts array
const posts = [];
posts.push({
post_id: newPost.post_id,
title: newPost.title,
});
const updatedUser = {
posts,
};
// update user to contain the posts array
await this.userService.edit(data.user_id, updatedUser); // error on updatedUser
export interface UserDTO {
user_id: string;
name: string;
posts: [
{
post_id: string;
title: string;
},
];
}
【问题讨论】:
-
明确输入帖子变量有帮助吗?
const posts: UserDTO['posts'] = [];。另外,我认为这不是您界面中的有效语法。试试posts: Array<{ post_id... }>; -
语法
[T]是有效的,但它是在谈论tuple,在这种情况下,是一个由T类型的元素组成的数组。如果您想要一个包含零个或多个T类型元素的数组,则应使用T[]或等效的Array<T>。
标签: typescript nestjs