在TypeScript中,我有以下类:
interface Post {
id: number;
tags: Tag[];
}
interface Tag {
id: number;
name: string;
}
我需要将帖子数组转换为帖子模型数组:
interface PostModel {
postId: number;
tagsNames: string[];
}
我尝试了以下操作:
let postModels : PostModel[] = posts.map((post: Post) => {
return {
postId: post.id,
tagNames: post.tags.map((tag: Tag) => tag.name)
};
});
但是我得到了错误:
Type '{ id: number; tagNames: string[]; }' is not assignable to type 'PostModel'.
Property 'tagsNames' is missing in type '{ id: number; tagsNames: string[]; }'.
我想念什么?