【发布时间】:2021-03-20 22:22:42
【问题描述】:
我有一个方法可以用.findOneAndUpdate 保存作者
AuthorInterface 看起来像这样:
export interface AuthorInterface {
name: string,
bio: string,
githubLink: string,
stackoverflowLink: string,
twitterLink: string,
image: string,
image_webp: string,
}
在这种情况下,我需要在这里做一个例外。 image 和 image_webp 是图像路径,因此我不能只覆盖这些值。我已经使用Omit 将其从AuthorInterface 参数中删除。
但是 typescript 仍然在 author: { ...author } 抱怨 image_webp 和 image 字段丢失。如何告诉 typescript 我不希望 image 和 image_webp 属性出现在参数对象中?
public saveAuthor(_id: mongoose.Types.ObjectId | string, author: Omit<AuthorInterface, "image" | "image_webp">): Promise<UserModelInterface | null> {
return User.findOneAndUpdate({ _id }, { author: { ...author } }, { new: true }).exec()
}
【问题讨论】:
标签: javascript mongodb typescript mongoose