Two data classes in Kotlin are very similar, with the only difference being that one contains an ID field while the other does not (the ID is generated only if the model is stored in the database).
data class RouteWithId(
val id: String,
val name: String,
val description: String,
val comments: List<Comment>,
val media: List<Media>,
val points: List<RoutePoint>,
val userId: String,
val status: RouteState,
val tracks: List<TrackInfo>,
) {
enum class RouteState(val value: String){
IN_REVIEW("in-review"),
PUBLISHED("published");
}
}
data class Route(
val name: String,
val description: String,
val comments: List<Comment>,
val media: List<Media>,
val points: List<RoutePoint>,
val userId: String,
val status: RouteState,
val tracks: List<TrackInfo>,
) {
enum class RouteState(val value: String){
IN_REVIEW("in-review"),
PUBLISHED("published");
}
}
An attempt was made to combine them with a nullable ID field, which resulted in unnecessary complexity since the ID is expected to exist in many instances (such as post-processing after database retrieval).
In TypeScript, there is a utility type called Omit that can derive a type from another type by excluding certain fields.
This approach would be ideal for this scenario, as it is known that the field will exist after database retrieval and will not exist before that.
Is there a way to achieve the same functionality in Kotlin?