Within a complex project encompassing multiple front-end applications and dozens of microservices, we maintain a private npm repository housing common components utilized by these front-end apps. These shared components include TypeScript models such as:
interface IUser {
id: string;
login: string;
name: string;
...
}
While this setup ensures consistency among the apps, the rapid development pace can result in discrepancies in the data returned by various microservices. For instance, a microservice might return an "enriched" object that extends the common interface:
interface IUser {
id: string;
login: string;
name: string;
...
extraField1: someType1;
extraField2: someType2;
}
To address such discrepancies, enriched types can be defined through interfaces that extend the base commons interface:
interface IUserEnriched extends IUser {
extraField1: someType1;
extraField2: someType2;
}
However, there are cases where a microservice returns a simplified object:
interface IUserImpoverished {
id: string;
/* no login: string; here; or may be it's optional, unlike in the common interface */
name: string;
...
}
I am curious if TypeScript offers a way to automatically generate such impoverished interfaces based on IUser
instead of duplicating code (?). This poses challenges when updating the commons: adding fields to IUser
may not affect all applications immediately, but removing a property like login
requires creating enriched interfaces wherever it is used. The ability to simplify interfaces would be beneficial in such scenarios.
I attempted researching using terms like "narrow [TS interface]" but couldn't find relevant information. If you are familiar with the correct terminology, please share your insights.