TypeScript interface with XOR, {bar:string} xor {can:number} [duplicate]

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)