0

I am trying to use subsets from enum keys as interface keys. No luck so far, I get the compiler error A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.

enum Operator {
  eq = 'eq',
  neq = 'neq',
  lt = 'lt',
  lte = 'lte',
  gt = 'gt',
  gte = 'gte',
  in = 'in'
}

interface Filter {
  range: {
    [Operator.gt | Operator.gte]: string, // <- error
    [Operator.lt | Operator.lte]: string // <- error
  }
}

Any idea how to solve that?

revy
  • 3,945
  • 7
  • 40
  • 85
  • 1
    Are you trying to type an exclusive or? As in, "this has to be GT, or GTE, but not both, and this has to be LT, or LTE, but not both"? – kelsny Sep 27 '22 at 18:14
  • @caTS That's correct – revy Sep 27 '22 at 18:16
  • 1
    [Here you go](https://stackoverflow.com/questions/42123407/does-typescript-support-mutually-exclusive-types) – kelsny Sep 27 '22 at 18:16
  • 1
    This doesn't seem to have much to do with enums, per se. You'd have the same problem trying to represent this with string literal types. You want something like [this](https://tsplay.dev/wRlo1m). (Or, for enums, [this](https://tsplay.dev/NVko5m), which amounts to the same thing). Do you want to see this as an answer? Or maybe we should just close as a duplicate of the question @caTS pointed to since that's the underlying issue – jcalz Sep 27 '22 at 18:23

0 Answers0