0

Is there a way in typescript to have a type that can be A or B but not a merge of both ?

See this example :

interface Equals {
   equals: string
}

interface NotEquals {
   notEquals: string
}

type Predicate = Equals | NotEquals

function usePredicate(value: any, predicate: Predicate) {
// ...
}

// OK usage
usePredicate("test", {
  equals: "test"
});

// OK usage
usePredicate("test", {
  notEquals: "tset"
});

// Is it possible to forbid this type ?
usePredicate("test", {
  notEquals: "tset",
  equals: "tset"
});

In the real code, I have way more that 2 possibilities (Equals / NotEquals) I would like a solution that stays easy to read and maintain

Bapt
  • 33
  • 6

0 Answers0