也许这不是执行此操作的最佳方法,但是我将枚举存储为字符串并将其声明为ts枚举。
尽管我似乎无法理解我从indexOf得到的错误。
enum MyTypes {
Cat,
Mat,
Sat
};
const example = ["Cat", "Mat"] as Array<keyof typeof MyTypes>;
const str1 = MyTypes[MyTypes.Sat] as string;
const str2 = "Sat";
//This causes error : Argument of type 'string' is not assignable to parameter of type '"Cat" | "Mat" | "Sat"'
console.log(example.indexOf(str1));
//This works fine
console.log(example.indexOf(str2));