safe-integer-ts
v1.0.0
Published
Newtype wrapper of number type as safe-integer (53-bit precise integer)
Downloads
20
Maintainers
Readme
SafeInteger for TypeScript
Provides SafeInteger
type, a newtype wrapper of number
as safe-integer or 53-bit precise integer.
See the implementations for more details.
Example
import { SafeInteger, asSafeInteger, isSafeInteger, toSafeInteger } from "safe-integer-ts"
const unknownValue: unknown = JSON.parse("42")
// As argument.
const useSafeInteger = (_value: SafeInteger) => {
// No need to validate in case of non-integer, NaN, integer-like string, etc.
}
// Validate.
{
if (isSafeInteger(unknownValue)) {
useSafeInteger(unknownValue)
}
}
// Cast.
{
const value: SafeInteger | null = asSafeInteger(unknownValue)
if (value != null) {
useSafeInteger(value)
}
}
// Convert.
{
const value: SafeInteger | null = toSafeInteger("42")
if (value != null) {
useSafeInteger(value)
}
}
ES module
import ... from "safe-integer-ts/esm"
// ^^^^