-
Notifications
You must be signed in to change notification settings - Fork 231
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
Exclusive Or and the Optional never Trick #16
Comments
Thanks Dan for this blog ! :) |
That's a cute idea (and mathematically correct!) but unfortunately TypeScript isn't able to follow along: type XOR<A, B> = Exclude<A | B, A & B>
type ExclusiveThing = XOR<ThingOne, ThingTwo>;
// ^? type ExclusiveThing = ThingOne | ThingTwo The issue is that the |
Hey @danvk! Thank you for mentioning ts-essentials. This type hasn't been added to TypeScript since 2017, you can read more in microsoft/TypeScript#14094 The utility type existed in ts-essentials for a long time but it didn't have a support of variadic XOR which last year was implemented in ts-xor - microsoft/TypeScript#14094 (comment) (It's also supported in ts-essentials@10) |
Here's a version that is perhaps a little more readable: type Xor<T, U> = (T & Never<T, U>) | (U & Never<U, T>);
type Never<T, U> = {
readonly [K in Exclude<keyof U, keyof T>]?: never;
}; Also worth noting that the definition can be easily extended to three or more types using either type Xor3<T, U, V> = (T & Never<T, U | V>) | (U & Never<U, T | V>) | (V & Never<V, T | U>); or, equivalently, type Xor3<T, U, V> = Xor<Xor<T, U>, V>; EDIT: Or, yeah, just use |
Exclusive Or and the Optional never Trick
Effective TypeScript: Exclusive Or and the Optional never Trick
https://effectivetypescript.com/2021/11/11/optional-never/
The text was updated successfully, but these errors were encountered: