3

Suppose I have the following type:

export type Step =
    | {name: 'a'}
    | {name: 'b'}
    | {name: 'c', foo: string}
    | {name: 'd'};

Is it possible to derive a tuple/array type that corresponds exactly to all the values of name in any order?

For example:

const stepNames = ['d', 'a', 'b', 'c']       // good, contains all values of `name` in any order
const stepNames = ['d', 'a', 'b']            // bad, `c` is missing
const stepNames = ['d', 'a', 'b', 'c', 'c']  // bad, `c` is repeated
Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
  • See [this](https://tsplay.dev/mLq5KW). Taken from [here](https://github.com/microsoft/TypeScript/issues/13298#issuecomment-692864087). Might be a duplicate of [this](https://stackoverflow.com/questions/69676439/create-constant-array-type-from-an-object-type/69676731#69676731) – captain-yossarian from Ukraine Jun 20 '22 at 07:11
  • 1
    @captain-yossarianfromUkraine I'm going to have to take some time to grok this, but however this works, it appears to be correct. Thanks a lot. This question does seem to be a dupe of the question you linked. It's slightly different, but close enough. Maybe vote to close? Thanks again. Слава Україні! – Dmitry Minkovsky Jun 20 '22 at 14:54
  • @captain-yossarianfromUkraine maybe leave the contents of your playground and an answer, though? – Dmitry Minkovsky Jun 20 '22 at 14:57

0 Answers0