I have a simple union type of string literals and need to check it\'s validity because of FFI calls to \"normal\" Javascript. Is there a way to ensure that a certain variabl
You can use "array first" solution to create string literals and use it as usual. And use Array.includes() at the same time.
const MyStringsArray = ["A", "B", "C"] as const;
MyStringsArray.includes("A" as any); // true
MyStringsArray.includes("D" as any); // false
type MyStrings = typeof MyStringsArray[number];
let test: MyStrings;
test = "A"; // OK
test = "D"; // compile error