A robust typed number library with strictness and conversion configuration.
t-nums was created as a stable and spec-aligned way to allow JavaScript codebases to maintain persistent numerical types and ensure interoperability between different systems that depend on typed numbers.
While you can use t-nums however you please, it is important to node the overhead and bloat from function calling, proper coercing of different types and configuration options for t-nums.
Only use this if you need persistent types for numbers. If you just want numbers that are only restricted in value, consider looking for other alternatives (preferably native solutions, such as Typed Arrays or DataViews).
import { uint8 } from "t-nums";
// Adding, casting and truncation
const a = uint8(200);
const b = uint8(100);
const c = +a + +b;
const d = uint8(+c);
console.log(c); // 300 (regular JavaScript number)
console.log(d); // 44 (casted and truncated to uint8)import { createTNumCtor } from "t-nums";
// Custom 12-bit typed number
const uint12 = createTNumCtor("uint12", (val) => {
if (typeof val === "bigint") val = Number(val);
return val & 0x07ff;
}, 0x07ff);
const a = uint12(600);
// ...