utility-types
utility-types copied to clipboard
XOR
Is your feature request related to a real problem or use-case?
This is from a long discussion in TS issues
https://github.com/microsoft/TypeScript/issues/14094?fbclid=IwAR05mMCDTNFE1xEB1dAbsfsqgDn5QS5ufDoYf1xyJJ8qdx7jStmHHn_KK0Q#issuecomment-373782604
Describe a solution including usage in code example
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;
type NameOnly = { is: "NameOnly", name: string };
type FirstAndLastName = { is: "FirstAndLastName", firstname: string; lastname: string };
type Person = XOR<NameOnly, FirstAndLastName>;
let person: Person;
person = { is: "NameOnly", name: "Foo" };
person = { is: "FirstAndLastName", firstname: "Foo", lastname: "Bar" };
let stringOrNumber: XOR<string, number>;
stringOrNumber = 14;
stringOrNumber = "foo";
let primitiveOrObject: XOR<string, Person>;
primitiveOrObject= "foo";
primitiveOrObject= { is: "NameOnly", name: "Foo" };
primitiveOrObject= { is: "FirstAndLastName", firstname: "Foo", lastname: "Bar" };
Who does this impact? Who is this for?
A lot of APIs that return one or another responses.
I'm curious to hear your perspective on this because you have a lot of experience with advanced generics
Just curious, how is this any different from using the basic or '|'? I know the difference between or and xor, but I don't see how this example would benefit from an XOR option.
Edit: I played around with the example some more and now typescript bothers me.
type NameOnly = { name: string };
type FirstAndLastName = {firstname: string; lastname: string };
type Person =NameOnly | FirstAndLastName;
const person: Person = {firstname: 'john', lastname: 'doe', name: 'micheal'}
This should be valid TS How is this in anyway what you would expect? Saying it is type A or B and than a weird mix of both is allowed!
XOR is really useful type. Built-in union type allows a mix of two types, which is unwanted behavior.
https://github.com/maninak/ts-xor