Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let XOR take multiple arguments? #22

Closed
christianalares opened this issue Jun 14, 2023 · 4 comments · Fixed by #27
Closed

Let XOR take multiple arguments? #22

christianalares opened this issue Jun 14, 2023 · 4 comments · Fixed by #27
Assignees
Labels
enhancement New feature or request

Comments

@christianalares
Copy link

christianalares commented Jun 14, 2023

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? (...)

@maninak
Copy link
Owner

maninak commented Jun 15, 2023

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 :))

@inoyakaigor
Copy link

As workaround I can propose to you these solution microsoft/TypeScript#14094 (comment)

@maninak
Copy link
Owner

maninak commented Aug 30, 2023

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).

@inoyakaigor
Copy link

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.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants