-
-
Notifications
You must be signed in to change notification settings - Fork 0
number types
export type Cast<N, T> = Extract<N, number> & T;Casts a value into a specific numeric type. If the value is not a number, it
will resolve to never, indicating its incompatibility with the type.
This is a low-level utility type that primarily serves as an internal helper for more user-friendly types like Integer and Positive. Unless you're building custom numeric types with your own branding, you most likely never directly handle this type in the wild.
NT
Numbers
import type { Cast } from "jsr:@nick/is/number";
declare const MY_DOUBLE: unique symbol;
type DOUBLE<N extends number = number> = Cast<N, typeof MY_DOUBLE>;
let x = 4.3210123210 as DOUBLE;
let y = 3.1415926535 as DOUBLE;
console.log(x + y); // 7.4626049745
// This will raise a TypeScript compiler error:
x = 1; // <- TS2322 Type '1' is not assignable to type 'DOUBLE'.export type CastInt<N, T> = Extract<N, bigint | `${bigint}`> & T;Casts a value into a specific integer type. If the value is not a bigint, it
will resolve to never, indicating its incompatibility with the type.
This is a low-level utility type that primarily serves as an internal helper for more user-friendly types like BigInteger or PositiveBigInteger. Unless you're building custom numeric types with your own branding, you most likely will never directly handle this type in the wild.
NT
Numbers
import type { CastInt } from "jsr:@nick/is/number";
declare const INTEGER: unique symbol;
type INTEGER<N extends bigint = bigint> = CastInt<N, typeof INTEGER>;
let x = 3n as INTEGER;
let y = 5n as INTEGER;
console.log(x + y); // 8nexport type Numeric = number | bigint | `${number | bigint}`;export type Unwrap<U> = U extends Cast<infer N, infer T> ? [N, T] : [U, never];Unwraps a type that has been cast with Cast
into a tuple of the original value and the specific type it was cast to (or
"branded" with). If the given type was not cast with
Cast, it will resolve to a tuple containing
the original value and never, respectively.
U
Numbers
import type { Cast, Unwrap } from "jsr:@nick/is/number";
type Int_3 = Cast<3, INTEGER>;
function unwrap<T>(value: T): Unwrap<T>[0] {
return value as Unwrap<T>[0];
}readonly IS_EVEN: true;readonly IS_INFINITE: false;readonly IS_FINITE: true;readonly IS_FLOAT: true;readonly PRECISION: 0.5;readonly PRECISION: 1;readonly PRECISION: 2;readonly IS_FINITE: false;readonly IS_INFINITE: true;readonly BIT_LENGTH: 16;readonly BIT_LENGTH: 32;readonly BIT_LENGTH: 64;readonly BIT_LENGTH: 8;readonly IS_INTEGER: true;readonly IS_EVEN?: true;readonly IS_INFINITE?: false;readonly IS_FINITE?: true;readonly IS_FLOAT?: true;readonly PRECISION?: 0.5;readonly PRECISION?: 1;readonly PRECISION?: 2;readonly IS_FINITE?: false;readonly IS_INFINITE?: true;readonly BIT_LENGTH?: 16;readonly BIT_LENGTH?: 32;readonly BIT_LENGTH?: 64;readonly BIT_LENGTH?: 8;readonly IS_INTEGER?: true;readonly IS_NAN?: true;readonly IS_POSITIVE?: false;readonly IS_NEGATIVE?: true;readonly IS_ZERO?: false;readonly IS_NON_ZERO?: true;readonly IS_ODD?: true;readonly IS_NEGATIVE?: false;readonly IS_POSITIVE?: true;readonly IS_UNSIGNED?: true;readonly IS_NON_ZERO?: false;readonly IS_ZERO?: true;readonly IS_NAN: true;readonly IS_POSITIVE: false;readonly IS_NEGATIVE: true;readonly IS_ZERO: false;readonly IS_NON_ZERO: true;readonly IS_ODD: true;readonly IS_NEGATIVE: false;readonly IS_POSITIVE: true;readonly IS_UNSIGNED: true;readonly IS_NON_ZERO: false;readonly IS_ZERO: true;