Monday 14 December 2020

Make an exepction at certail fields

I have an method that saves an author with the .findOneAndUpdate

AuthorInterface looks like this:

export interface AuthorInterface {
    name: string,
    bio: string,
    githubLink: string,
    stackoverflowLink: string,
    twitterLink: string,
    image: string,
    image_webp: string,
}

In this case i need to make an exepction here. image and image_webp are image paths and therefore i cannot just override the values. I already used Omit to remove it out of the AuthorInterface argument.

However typescript still complains at author: { ...author } that iamge_webp and image field is missing. How do i tell typescript that i dont expect image and image_webp properties to be 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()
}


from Make an exepction at certail fields

No comments:

Post a Comment