I'd like to make a property optional based on the generic type. I've tried the following:
interface Option<T extends 'text' | 'audio' | 'video'> {
id: string;
type: T;
text: T extends 'text' ? string : undefined;
media: T extends 'audio' | 'video' ? T : undefined;
}
const option: Option<'text'> = { text: "test", type: "text", id: "opt1" };
So the idea is that the property text only is defined for Option<'text'> and media only is defined for Option<'audio' | 'video'>.
However, the ts compiler gives me the following error:
Property 'media' is missing in type '{ text: string; type: "text"; id: string; }'
but required in type 'Option<"text">'.ts(2741)
How can I work around this?