I am currently exploring a method for incorporating default and virtual values in a database schema. The concept involves using a utility type to automatically convert a schema for insertion (props with defaults or generated by virtuals aren't necessary) and as a query result (ensuring that these fields will be populated by defaults or other "virtual"-like settings so they can be classified as NonNullable).
For example, simplifying typings to a single prop:
type WithDefault<T> = T;
// Mark prop as optional
type Input<T> = T extends WithDefault<infer T> ? T | undefined : T;
The challenge lies in the fact that there is no distinction between Input<WithDefault> and Input.
Do you have any suggestions on how to achieve a pattern like this? The goal is to have a schema similar to:
type User = {
firstName: string
lastName: string
fullName: WithDefault<string>
}
This would allow for seamlessly transitioning between an insertable schema and a readable schema.