enum Month {
Jan='January',
Feb = 'February',
Mar='March'
}
console.log(Month.Feb);
for(var n in Month) {
console.log(n);
console.log(Month[n]);
}
输出如下:
[LOG]: "February"
[LOG]: "Jan"
[LOG]: "January"
[LOG]: "Feb"
[LOG]: "February"
[LOG]: "Mar"
[LOG]: "March"
但是编译器总是“提示”/“报错”:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Month'. No index signature with a parameter of type 'string' was found on type 'typeof Month'.
string 不能用于 enum 访问的 index 么?
但是结果,似乎又能够正常工作。不是很理解。
又比如:
const a : Month = Month["Jan"];
console.log(a);
console.log(typeof a);
[LOG]: "January"
[LOG]: "string"
我已经指定 a 的 type 是 Month,编译器没有报错,但是打印出来,typeof a 是 string
翻了半天 typescript 的文档,貌似也没有解释得很清楚。