Here is a snippet of code I'm working with:
interface Notification {
message: TemplatedEmail & Email, //current attempt which doesnt do what I want
}
interface Destination {
ccAddresses?: string[],
bccAddresses?: string[],
toAddresses: string[]
}
interface TemplatedEmail {
destination: Destination,
source: string,
template: string,
templateData: any,
replyToAddresses?: string[]
}
interface Email {
destination: Destination,
source: string,
body: string,
subject: string,
replyToAddresses?: string[]
}
I am trying to ensure that the message
property of Notifications
can be either of type Email
or TemplatedEmail
. This means that all properties (optional ones too) of either Email
should be present in message
, or all properties of TemplatedEmail
. With Union Types, I can only access common properties, and with intersection types, I get all properties from both.
The issue arises when trying something like this:
const x: Notification = {
message: {
destination: { toAddresses: [ "" ] },
source: "",
body: "",
subject: ""
}
};
This causes an error indicating that the properties template
and templateData
are missing from x
.