Skip to content

Instantly share code, notes, and snippets.

@bamorim
Created April 9, 2021 20:23
Show Gist options
  • Save bamorim/b260cb99c2a463348450f9fb9647d40c to your computer and use it in GitHub Desktop.
Save bamorim/b260cb99c2a463348450f9fb9647d40c to your computer and use it in GitHub Desktop.
xorts
type XOR<T, U> = (T | U) extends object
? (Without<T, U> & U) | (Without<U, T> & T)
: T | U
type Without<T, U> = {
[P in Exclude<keyof T, keyof U>]
?
: never
}
type WithHeight = { height: number }
type WithWidth = { width: number }
type OtherProps = { color?: string }
type Props = OtherProps & XOR<XOR<WithHeight, WithWidth>, {}>
const p1: Props = { color: "aaa" }
const p2: Props = { color: "aaa", width: 1 }
const p3: Props = { color: "aaa", height: 1 }
const perr: Props = { color: "aaa", height: 1, width: 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment