I have a unique situation where I am working with an interface that is based on a schema definition provided by a validation library. This schema includes some readonly
values that are meant to be set by the server. However, when I am preparing data to send to the server, I want to reuse this interface as much as possible. In an ideal scenario, I would like to create a type transformation like:
type RemoveReadonly<T> = // ???;
// And apply it like
interface IFoo {
a: number;
readonly b: string;
}
type Transformed = RemoveReadonly<T>; // { a: number; }
To handle this, I am implementing a builder pattern for constructing data to be sent to the server. It would be beneficial to only allow setting mutable values in the internal partial representation I am creating, in order to prevent errors such as mistakenly sending a value for a readonly
field at compile time.
I have been struggling to find an effective way to achieve this. While TypeScript does provide methods to eliminate the readonly
property from an interface (to generate a Writable
or Mutable
version of the interface), there seems to be no straightforward approach to filter out or Exclude
these values.