3

Given:

type State = 
  | 'read'
  | 'rejected'
  | 'sent'
  | 'refused'
  | 'expired' 
  | 'draft'

Needed:

the template literal type Filters, which defines a string of this kind: darft,sent,refused.

type Filters = 
  | `${State}`
  | `${State},${State}`
  | `${State},${State},${State}`
// ...
// allowing strings containing from 1 to all of the `State`s.
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Chaika
  • 73
  • 5
  • 1
    No repeats like `draft,draft`, right? Will `State` always and forever have only 6 members? You can indeed represent a `CommaSeparated` type function that comes up with every permutation of at least one element of members of `T` separated by commas, like [this](https://tsplay.dev/mxDBZm)... but that operation is *combinatorially explosive*; for a `T` with elements, `CommaSeparated` has around ×!, for 6 elements that's 1,956 members, and TypeScript can handle that. But for 9 elements that's 986,409 members, and TS will complain. – jcalz Jun 10 '22 at 17:22
  • 1
    Please see the answer to the linked questions for more information... one answer says how to generate the huge union, while the other says how to make a generic type *constraint* instead, which is appropriate when the huge union would be too big to represent in TypeScript. – jcalz Jun 10 '22 at 17:43

0 Answers0