Reputation: 533
I have the following class:
export class User {
id: string;
updatedAt?: Date;
createdAt: Date;
email: string;
emailVerified: boolean;
firstName?: string;
lastName?: string;
dateOfBirth?: Date;
}
And I now want to create another type that picks a few properties and has FieldValue as a type Union on every property:
export type UpdateUser = {
email?: string | FieldValue;
emailVerified?: boolean | FieldValue;
firstName?: string | FieldValue;
lastName?: string | FieldValue;
dateOfBirth?: Date | FieldValue;
};
Is there any way to do this other than to manually define the type unions on every property?
Best Philipp
Upvotes: 2
Views: 1100
Reputation: 33041
sure there is a wayt to do it:
type FieldValue = { age: number };
export type Initial = {
email?: string;
emailVerified?: boolean;
firstName?: string;
lastName?: string;
dateOfBirth?: Date;
};
// This is our helper
type Unionize<T, K> = {
[P in keyof T]: T[P] | K
}
type UpdateUser = Unionize<Initial, FieldValue>
Upvotes: 2