Skip to content

Instantly share code, notes, and snippets.

@jasonrhodes
Created December 14, 2018 00:40
Show Gist options
  • Save jasonrhodes/2a9646f82907982f85f6342e1398158c to your computer and use it in GitHub Desktop.
Save jasonrhodes/2a9646f82907982f85f6342e1398158c to your computer and use it in GitHub Desktop.
type Neverize<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type ExclusiveOr<T, U> = (T | U) extends object
? (Neverize<T, U> & U) | (Neverize<U, T> & T)
: T | U;
interface S {
value: string;
string: string;
}
interface N {
value: number;
number: string;
}
function what(x: ExclusiveOr<S, N>) {
return x.value;
}
what({ value: 'asdf', string: 'asdf' });
what({ value: 5, string: 'asdf' });
what({ value: 5, number: 'asdf' });
what({ value: 'asdf', number: 'asdf' });
what({ value: 'asdf', string: 'asdf', number: 'asdf' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment