I have a requirement for a custom Partial<T>
type that can transform nullable fields into optional ones. Currently, I am focusing on typing our ORM, which converts undefined values to null for nullable fields.
In particular, I want to modify a type like this:
interface User {
email: string
name: string | null
}
To look like this:
interface User {
email: string
name?: string | null
}
I attempted to create the following type:
type NullablePartial<T> = { [P in keyof T]: T[P] extends null ? T[P] | undefined : T[P] }
However, using extends null
did not function as intended for creating a union with null
and other types (I want it to work with more than just strings). Also, distinguishing between optional fields and undefined values is crucial to me.
Any suggestions on how to proceed?