You can use a union type to specify that the type can have either one prop or the other
type Button = {
href: string;
action?: never
} | {
action: () => void;
href?: never;
}
let b1: Button = { href: "" };
let b2: Button = { action: () => { } };
let b3: Button = { action: () => { }, href: "" }; // error
You need to add the optional never
props to ensure you get an error, this is due to the way excess property checks work you can read more here. You could also use the StrictUnion
type in that answer, but for just two properties it might be overkill.