58

I have a method that takes a parameter. I would like Typescript to verify that the object being passed in (at typescript compile-time, I understand run-time is a different animal) only satisfies one of the allowed interfaces.

Example:

interface Person {ethnicity: string;}
interface Pet {breed: string;}
function getOrigin(value: Person ^ Pet){...}

getOrigin({}); //Error
getOrigin({ethnicity: 'abc'}); //OK
getOrigin({breed: 'def'}); //OK
getOrigin({ethnicity: 'abc', breed: 'def'});//Error

I realize that Person ^ Pet is not valid Typescript, but it's the first thing I thought to try and seemed reasonable.

2
  • I think what you're looking for is Type Guards. AFAIK TypeScript doesn't support exclusive types.
    – user47589
    Feb 8, 2017 at 21:15
  • For people looking to keep only one property among all the keys of an interface, I suggest using oneOf Dec 5, 2020 at 14:31

7 Answers 7

74

As proposed in this issue, you could use conditional types to write a XOR type:

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;

And now your example works:

interface Person {ethnicity: string;}
interface Pet {breed: string;}
function getOrigin(value: XOR<Person, Pet>) { /* ... */}

getOrigin({}); //Error
getOrigin({ethnicity: 'abc'}); //OK
getOrigin({breed: 'def'}); //OK
getOrigin({ethnicity: 'abc', breed: 'def'});//Error
6
  • 7
    That XOR is so handy! How could this be extendd to support multiple mutualy exclusive types? Could you extend your answer to support XOR<Person, Pet, Car, Tree, ..., House> ?
    – maninak
    Feb 21, 2019 at 0:46
  • 1
    One would need to build up an according chain (I suspect it works because of transitivity?!): type XOR3<S, T, U> = XOR<S, XOR<T, U>>;
    – Newlukai
    Oct 16, 2019 at 10:15
  • 10
    @maninak A little late to the party, but this (TS playground) would accomplish what you were looking for with OneOf<[Person, Pet, Car, Tree, ..., House]>
    – tjjfvi
    May 27, 2020 at 0:34
  • 2
    Horrifying @tjjfvi... :) Jun 18, 2020 at 13:09
  • 6
    @ConnorDooley Yes, I did. It was a while ago, so here's a cleaner version: tsplay.dev/wgLpBN
    – tjjfvi
    Oct 26, 2021 at 22:09
14

You can use the tiny npm package ts-xor that was made to tackle this problem specifically.

With it you can do the following:

import { XOR } from 'ts-xor'
 
interface A {
  a: string
}
 
interface B {
  b: string
}
 
let A_XOR_B: XOR<A, B>
 
A_XOR_B = { a: 'a' }          // OK
A_XOR_B = { b: 'b' }          // OK
A_XOR_B = { a: 'a', b: 'b' }  // fails
A_XOR_B = {}                  // fails

Full disclosure: I'm the author of ts-xor. I found that I needed to implement the XOR type from repo to repo all the time. So I published it for the community and me and in this way, I could also add tests and document it properly with a readme and jsdoc annotations. The implementation is what @Guilherme Agostinelli shared from the community.

4

To augment Nitzan's answer, if you really want to enforce that ethnicity and breed are specified mutually exclusively, you can use a mapped type to enforce absence of certain fields:

type Not<T> = {
    [P in keyof T]?: void;
};
interface Person {ethnicity: string;}
interface Pet {breed: string;}
function getOrigin(value: Person & Not<Pet>): void;
function getOrigin(value: Pet & Not<Person>): void;
function getOrigin(value: Person | Pet) { }

getOrigin({}); //Error
getOrigin({ethnicity: 'abc'}); //OK
getOrigin({breed: 'def'}); //OK

var both = {ethnicity: 'abc', breed: 'def'};
getOrigin(both);//Error
2

You can use Discriminating Unions:

interface Person {
  readonly discriminator: "Person"
  ethnicity: string
}

interface Pet {
  readonly discriminator: "Pet"
  breed: string
}

function getOrigin(value: Person | Pet) { }

getOrigin({ }) // Error
getOrigin({ discriminator: "Person", ethnicity: "abc" }) // OK
getOrigin({ discriminator: "Pet", breed: "def"}) // OK
getOrigin({ discriminator: "Person", ethnicity: "abc", breed: "def"}) // Error
1

As of TS v4.7, I found Omit to be the simplest solution:

interface Circle {
    radius: number;
}

interface Polygon {
    sides: number;
}

type Either<A, B> = Omit<A, keyof B> | Omit<B, keyof A>;

const mutuallyExclusiveProps: Either<Circle, Polygon> = { radius: 5 };

mutuallyExclusiveProps.sides = 5; // Error
3
  • 1
    Works fine only until you don't have a common property in both of them, say perimeter. Then, you won't be able to pass neither a valid Circle nor a valid Polygon into the thing ;) Jul 21, 2022 at 14:09
  • Correct. I thought that was the whole reason for this type to exist. I personally use this trick in my front-end projects to define strictly mutually exclusive props for a particular component which I can then go and spread. Jul 22, 2022 at 15:45
  • Yeah, that would definitely be one of the use cases, but I stumbled into this post while looking for a way to structure a tree graph, say, with leaf nodes that have value, or branch nodes that have children, but also leaves and branches could have properties in common, inherited from BaseNode. So in a resulting structure I wanted XOR<Branch, Leaf>, so to guarantee there can't be children and value at the same time. I solved it through an explicit never prop BTW, so children?: never in a Leaf and value?: never in a Branch and it worked fine. Jul 23, 2022 at 21:19
1

For a bigger list of options you can use StrictUnion:

type UnionKeys<T> = T extends T ? keyof T : never;
type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;
type StrictUnion<T> = StrictUnionHelper<T, T>

type WhatYouWanted = StrictUnion<Person | Pet | Car>
-3

You can use union types:

function getOrigin(value: Person | Pet) { }

But the last statement won't be an error:

getOrigin({ethnicity: 'abc', breed: 'def'}); // fine!

If you want that to be an error then you'll need to use overloading:

function getOrigin(value: Pet);
function getOrigin(value: Person);
function getOrigin(value: Person | Pet) {}
1
  • 2
    Union types doesn't solve the problem and is kinda what the OP is asking what to replace it with, but function overload works very nicely for this use case.
    – Meligy
    May 20, 2018 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.