I have the following TypeScript interface that is used as a database entity for an ORM library:
export interface Entity {
id?: number;
someColumn: string;
someOtherValue: number;
otherColumn: string;
}
Notice the optional id
property, which is either the primary key of the entity or undefined.
If it is undefined, then this means that the entity does not exist in the underlying database.
However, many functions only accept Entity
-objects that have a valid id.
Therefore, I would like to introduce a new interface that looks like this (without the "?"):
export interface ValidEntity {
id: number;
someColumn: string;
someOtherValue: number;
otherColumn: string;
}
Now my problem is that I do not want to duplicate all the properties from the original Entity
-interface.
How can I "extend" the Entity
-interface with a constraint to enforce that id
must not be undefined?
Reversing The Question
Another question is the same thing in the opposite direction.
Suppose that we already have the ValidEntity
interface and want to create an Entity
interface that relaxes the id
property to allow undefined. How can we accomplish this relaxation without duplicating properties?