在 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[]; }'.
我错过了什么?