I have a situation where I need to send a backend model to the frontend with certain properties excluded, and I want to use typing to ensure these exclusions.
My initial attempt involved creating an interface for the model and then a restricted interface using Omit<> to exclude specific properties.
interface Thing {
foo: string
secret: string
}
interface RestrictedThing extends Omit<Base, "secret"> {}
const thing: Thing = { foo: "test", secret: "must_exclude_in_restricted"}
const restricted: RestrictedThing = { ...thing } //no warning is raised
However, TypeScript does not give a warning when using the spread operator to copy properties, even if they should not be part of RestrictedThing.
I came across ts-essentials StrictOmit, but it only validates the properties passed in, making it unsuitable for my needs.
I also attempted to use typeof but without success.
declare var { bar, ...forRestricted }: Thing;
type RestrictedThing = typeof forRestricted;
Is there a reliable method to create a copy of my Thing object that uses typing to exclude unwanted properties?
Any alternative approaches would be appreciated.