javascript - 如何在打字稿中使用 bigint 初始化程序创建一个类似枚举的对象?
问题描述
我最理想的做法是:
enum Distance {
Far: 0n,
Medium, // 1n
Close, // 2n
}
function Calculate(length: Distance) {
// do something here
}
不幸的是,枚举还不支持 bigint,所以这不起作用。
我试过类似的东西:
const Distance = {
Far: 0n,
Medium: 1n,
Close: 2n
}
type Distance = typeof Distance;
function Calculate(length: Distance) {
const answer = 1n + length;
// Operator '+' cannot be applied to types 'bigint' and '{ Far: bigint; Medium: bigint; Close: bigint }'
}
但这似乎不起作用(我不能像使用普通枚举那样使用它)。
我知道我可以将数字转换为 bigint ,BigInt()
但我宁愿不这样做。
我如何创建功能类似于枚举的东西,因为它使用 bigints 而不是数字或字符串?
解决方案
推荐阅读
- multithreading - 如何使用 self 中的方法使用函数?
- blockly - 为什么 Blockly 不为自定义块生成代码?
- node.js - React get with axios request 显示来自 api 的数据不正确
- reactjs - 如何使用 Typescript 在 React 上允许非声明性命名空间
- sql - 按 id 和最近的时间戳合并 2 个数据帧
- matrix - 进行矩阵块乘法的更有效方法
- laravel - 使用 laravel 的一对多关系的 Eloquent 构建器实例上不存在属性 []
- mysql - 使用的索引列列表 (mysql/mariadb)
- matlab - 网络窗口无效。它被用户关闭或进程终止
- flutter - Flutter 更改它在选项卡中显示的内容