2

Imagine a function that get an object as argument that has two entries with any possible type.

const x = ({a,b}:{a: string, b: string}) => {
    // return something
}

x({a: "foo"}) // OK
x({b: "bar"}) // OK
x({a: "foo", b: "bar"}) // get a compile error in this case!

How can we set this function in a way that only one of the entries be passed to function? In other words, I need to get a compile error when both entries are set inside the function? For example, if "a" is already passed, the moment "b" is also passed we get a compilation error and vice versa.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Amin Jafarlou
  • 336
  • 1
  • 10

1 Answers1

2

My original intuitive, and obviously wrong, answer: Use a union type:

{ a: string } | { b: string }

This will not trigger a compile error for x({a: "foo", b: "bar"}).

Working solution: You can follow this advice for “mutually exclusive types”:

type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;

const x = (args: XOR<{ a: string }, { b: string }>) => {
    // return something
}

x({a: "foo"}) // OK
x({b: "bar"}) // OK
x({a: "foo", b: "bar"}) // get a compile error in this case!

See TypeScript Playground

qqilihq
  • 10,794
  • 7
  • 48
  • 89
  • Thanks for your answer and it works almost perfect. However, there is one small issue, if we pass 'b' as first entry ( for example: ``` x({b: "foo", a: "bar"}) ``` ), we get a compile error for "b" again, but we should get it for "a" this time. – Amin Jafarlou Jul 19 '22 at 07:13
  • I'm definitely not an expert in this field, but I doubt that you can influence this behavior. – qqilihq Jul 19 '22 at 09:52