How do I say that I want an interface to be one or the other, but not both or neither?
interface IFoo { bar: string /*^XOR^*/ can: number; }
You can use union types along with the never
type to achieve this:
type IFoo = { bar: string; can?: never } | { bar?: never; can: number }; let val0: IFoo = { bar: "hello" } // OK only bar let val1: IFoo = { can: 22 } // OK only can let val2: IFoo = { bar: "hello", can: 22 } // Error foo and can let val3: IFoo = { } // Error neither foo or can
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With