I want to write a type that allows for object creation with some core properties that are always required and an optional property imageUrl
which when defined as a string means imageAltText
is also required as a string,
{
id: "test",
imageUrl: "test",
imageAltText: "test"
}
I also want it to work so that when imageUrl
is not defined I do not expect imageAltText
to be defined.
{
id: "test"
}
I've defined the type as the following,
(
{
/** image url for the banner of the card. Will display a blue background without an image */
imageUrl?: undefined;
}
|
{
/** image url for the banner of the card. Will display a blue background without an image */
imageUrl: string;
/** alternative text for the banner image */
imageAltText: string;
}
)
However, by making `imageUrl` optional `?` typescript allows me to write `imageAltText` even when `imageUrl` is undefined.