I'm currently exploring how to merge declare an interface, with the twist of adding a property to the object literal type instead of directly to the interface itself.
Within a library, I have a type that looks like this:
interface DefaultSession {
user?: {
name?: string | null;
email?: string | null;
image?: string | null;
};
expires: ISODateString;
}
If I wanted to include a field within this interface without altering the original definition, I could reopen and declare it like so:
interface DefaultSession {
role?: string
}
However, my challenge lies in merging declare 'role' as a property under 'user', resulting in a type structured as follows:
interface DefaultSession {
user?: {
name?: string | null;
email?: string | null;
image?: string | null;
role?: string | null;
};
expires: ISODateString;
}