我想知道为什么可以将数字枚举分配给任何数字。数字枚举和基于字符串的枚举有什么区别?
enum TYPES {
False = 0,
True = 1,
UnKnow = 2,
}
type IType = {
type: TYPES
}
const demo1: IType = {
type: 9 // works, hope error: Type 9 is not assignable to type TYPES
}
enum COLORS {
GREEN = 'green',
RED = 'red',
YELLOW = 'yellow',
}
type IColor = {
color: COLORS;
}
const demo2: IColor = {
color: 'blue' // error: Type '"blue"' is not assignable to type 'COLORS'.ts(2322)
}