One of my methods involves the saving of an author using the .findOneAndUpdate
function.
The structure of AuthorInterface
is as follows:
export interface AuthorInterface {
name: string,
bio: string,
githubLink: string,
stackoverflowLink: string,
twitterLink: string,
image: string,
image_webp: string,
}
In this particular scenario, I encounter a unique situation. The properties image
and image_webp
contain paths to images, making it impossible to simply overwrite their values. To address this, I have utilized Omit
to exclude them from the AuthorInterface
argument.
Despite that, TypeScript continues to raise errors when I use author: { ...author }
, indicating that the fields image_webp
and image
are missing. How can I communicate to TypeScript that these two properties are not expected in the argument object?
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()
}