Conversation
source/xor.d.ts
Outdated
| Returns `false` if the giving type is `never`, else return it. | ||
| */ | ||
| type IfNever<T> = [T] extends [never] ? false : T; | ||
| // TODO: use to simplify `And`, `Or` types |
There was a problem hiding this comment.
Do you mean rewrite And and Or with Xor? I feels it makes more sense to write Xor using And and Or, like:
type Xor<A extends boolean, B extends boolean> = Or<And<Not<A>, B>, And<A, Not<B>>>;But this would currently not work because Not doesn't handle never properly. So, your implementation is fine, just remove this comment.
There was a problem hiding this comment.
Ohh no, the comment point to IfNever that i want to use to simplify this pattern
type T = _Or<If<IsNever<A>, false, A>, If<IsNever<B>, false, B>>which is used in Or and repeats multiple unnecessary checks trough the process and IfNever is a custom private types between Xor and Or which directly infer the result:
type T = _Or<IfNever<A>, IfNever<B>>There was a problem hiding this comment.
I was going to use And and Or for this, but the extra conditions didn’t make much sense to me — why prettify things and lose performance?
A working Xor would look like this:
type Xor<A extends boolean, B extends boolean> = And<Or<A, B>, Not<And<A, B>>>;There was a problem hiding this comment.
Ohh no, the comment point to
IfNeverthat i want to use to simplify this pattern
No, please remove this abstraction and use <If<IsNever<T>, false, T>, similar to how it’s done in Or:
Lines 78 to 79 in 3bd9de6
It's quite simple and easy to understand and doesn't need further abstraction.
There was a problem hiding this comment.
I was going to use
AndandOrfor this, but the extra conditions didn’t make much sense to me — why prettify things and lose performance?
There's NO performance loss as such, and it's actually better doing it with And and Or because it helps validate that And and Or themselves are implemented correctly. But, it's fine for now.
There was a problem hiding this comment.
Okay, I'll use ur seggestion for IfNever.
For the And and Or version of Xor it does work perfectly, I test it. If it's better to make it this way tell me.
There was a problem hiding this comment.
For the
AndandOrversion ofXorit does work perfectly, I test it. If it's better to make it this way tell me.
Yeah, let's do it using And, Or & Not.
Either definition works:
-
A working
Xorwould look like this:type Xor<A extends boolean, B extends boolean> = And<Or<A, B>, Not<And<A, B>>>;
-
I feels it makes more sense to write
XorusingAndandOr, like:type Xor<A extends boolean, B extends boolean> = Or<And<Not<A>, B>, And<A, Not<B>>>;
Co-authored-by: Som Shekhar Mukherjee <49264891+som-sm@users.noreply.github.com>
adding
Xortype (remake #1156 )