7

I have a string literal union type inherited from a library, and it has hundreds of members (the IconName type from @fortawesome/fontawesome-common-types, if you're curious). I am not able to redefine this type or use an enum instead.

I know that you can't convert a string literal union to an Array. But is there some way to use types to ensure that an array contains all the possible values of a string literal union?

I can do something like

const iconNames: IconName[] = [
  ...
]

but that only guarantees that all members of the array are valid IconName, it doesn't guarantee that the list is exhaustive.

Is there a way to exhaustively confirm that all members of an array conform to a type, and that the array lists all possible values of the type?

As I said, I don't control this type so I can't just rewrite it as an enum, or rewrite it as an array then derive the union type from the array. The intended use case here is to give users a dropdown to choose an icon from the list of possible valid values.

chadlavi
  • 105
  • 1
  • 6

0 Answers0