Let `XOR` take multiple arguments?
christianalares opened this issue · comments
There is a nice example of how to use this with more than two types:
// example1.ts
import type { XOR } from https://github.com/maninak/ts-xor
interface A {
a: string
}
interface B {
b: string
}
interface C {
c: string
}
let test: XOR<A, XOR<B, C>>
test = { a: '' } // OK
test = { b: '' } // OK
test = { c: '' } // OK
test = { a: '', c: '' } // rejected
test = {} // rejected
Is there a possibility to make the helper more dynamic so you can pass more than two arguments to this helper such as:
type A = { a: string }
type B = { a: string }
type C = { a: string }
type D = { a: string }
let test: XOR<A, B, C, D>
test = { a: '' } // OK
test = { b: '' } // OK
test = { c: '' } // OK
test = { d: '' } // OK
test = { a: '', b: '' } // Rejected
test = {} // Rejected
I'm not a TS expert but isn't there away to spread types using the spread syntax? (...
)
Hey Christian, thanks for coming over and sharing your thoughts!
I've looked into this in the past and even though I'd get the syntax to work in some cases, there would be regressions in others.
I'm a bit tight on time, but I'll look into it when possible. I'll leave the issue open regardless, in case someone can solve this puzzle and PR it in the meantime :))
As workaround I can propose to you these solution microsoft/TypeScript#14094 (comment)
As workaround I can propose to you these solution microsoft/TypeScript#14094 (comment)
That's interesting (I haven't actually tested that it works as expected without regressions). Preferably, though, the feature would not be a breaking change (nor a new type would be added).
Something like this would expand the current XOR
type to just accept more than two arguments (like Christian proposed in the issue description).
Just to clarify: under the word "you" I meant @christianalares
About AllXOR. I used it in a few projects since 2020 and it works as expected but VSCode tooltips look ugly as F.