本文详解如何在 typescript 中安全、类型完备地使用 bigint 实现位运算权限系统,涵盖 enum 限制、const 对象替代方案、类型定义技巧及关键注意事项。
本文详解如何在 typescript 中安全、类型完备地使用 bigint 实现位运算权限系统,涵盖 enum 限制、const 对象替代方案、类型定义技巧及关键注意事项。
TypeScript 原生 enum 不支持 bigint 字面量(如 0n)作为成员值——这是语言层面的硬性限制,尝试写 enum Perms { None = 0n } 会直接报错。因此,若需基于 BigInt 实现位运算权限(如 role.permissions & Perms.Read !== 0n),必须采用更灵活、类型友好的替代方案。
使用 const 对象配合 as const 和类型推导,既保证运行时值为 BigInt,又提供精确的编译时类型:
export const Perms = { None: 0n, Read: 1n, Write: 2n, Execute: 4n, Admin: 8n,} as const;// 自动推导出精确字面量联合类型:0n | 1n | 2n | 4n | 8nexport type Perm = typeof Perms[keyof typeof Perms];
这样定义后,Perm 类型即为所有权限值的精确联合类型,完全等价于手动书写 type Perm = 0n | 1n | 2n | 4n | 8n,但无需重复维护。
注意:BigInt 运算结果必须与 0n(而非 0)比较,否则类型检查失败且逻辑错误:
function hasPermission(role: Role, permission: Perm): boolean { return (role.permissions & permission) !== 0n; // ✅ 正确:0n // return (role.permissions & permission) !== 0; // ❌ 错误:number vs bigint}
调用示例:
if (hasPermission(currentUser, Perms.Read)) { console.log("允许读取");}
综上,放弃 enum,拥抱 const + as const + 类型提取 模式,是 TypeScript 中处理 BigInt 权限系统的最简洁、最健壮、最符合类型工程规范的实践路径。