- enum只用来声明数字就行了,字符串用联合类型最好,enum的值不能是对象类型
- enum
enum A = {
todo = 0,
done, // 1
deleted // 2
}
let status1 = A.todo
let status2 = 0
enum Permission = {
None = 0,
Read = 1 << 0, // 0001
Write = 1 << 1, // 0010
Delete = 1 << 2, // 0100
Manage = 1 << 3 // 1000
}
type User = {
permission: Permission
}
const user: User = {
permission: 0b0101
}
if(CanWrite(user.permission)){
console.log('有写权限')
}
if(CanManage(user.permission)){
console.log('有管理权限')
}
function CanWrite(p:Permission){
return (p & Permission.Write) === Permission.Write
}
function CanManage(p:Permission){
return (p & Permission.Manage) === Permission.Manage
}
网友评论