// Detail see: https://stackoverflow.com/questions/42123407/does-typescript-support-mutually-exclusive-typestypeWithout<T,U>={[PinExclude<keyofT,keyofU>]?:never};typeXOR<T,U>=(T|U)extendsobject?(Without<T,U>&U)|(Without<U,T>&T):T|U;classBothString{constructor(publica:string,publicb:string){}}classBothNumber{constructor(publica:number,publicb:number){}}typeBothStringOrNumber=XOR<BothString,BothNumber>// Now the function body is simplefunctionf(input:BothStringOrNumber){if(inputinstanceofBothString){returninput.a+':'+input.b;}else{returninput.a+input.b}}// Behave as expectf({a:2,b:3});// correct usagef({a:1,b:'a'});// error!f({a:'a',b:2});// error!f({a:'a',b:'b'})// correct usage
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I find another solution: