Given an array of tokens:
const myArray = <const>[
{
tokens: ["one", "two"],
},
{
tokens: ["three"],
},
{
tokens: [],
},
{
somethingElse: {},
},
];
I want to get a tuple for the all the different literals that the tokens property has. In the above example the result should be "one" | "two" | "three", however all my attempts yield any, unless I remove the third array item.
This is what I tried:
type ArrayItem = typeof myArray extends (infer T)[] ? T : never;
type ArrayItemWithTokens = Extract<ArrayItem, { tokens: [string] }>;
type TokenArrays = ArrayItemWithTokens extends { tokens: infer T } ? T : never;
type Token = TokenArrays extends (infer T)[] ? T : never;