I have an array of given union type, then wants to check if a string from a superset of the union type is contained in the array (runtime check):
I have found a potentially safer solution to this problem:
const knownStrings = { abc: true, def: true, }; const isKnownString = (name: string): name is keyof typeof knownStrings => name in knownStrings;
This doesn't require any typecasting, the only downside is the redundant true values.
true