ts-fortress
    Preparing search index...

    Variable SafeInt

    SafeInt: {
        abs: (
            x: WithSmallInt<SafeInt, 40>,
        ) => IntersectBrand<SafeInt, NonNegativeNumber>;
        add: (
            x: WithSmallInt<SafeInt, 40>,
            y: WithSmallInt<SafeInt, 40>,
        ) => SafeInt;
        div: (
            x: WithSmallInt<SafeInt, 40>,
            y:
                | 20
                | 1
                | 2
                | 10
                | 3
                | 4
                | 5
                | 6
                | 7
                | 8
                | 9
                | 11
                | 30
                | 18
                | 28
                | 38
                | 12
                | 13
                | 14
                | 15
                | 16
                | 17
                | 19
                | 21
                | 22
                | 23
                | 24
                | 25
                | 26
                | 27
                | 29
                | 31
                | 32
                | 33
                | 34
                | 35
                | 36
                | 37
                | 39
                | -20
                | -1
                | -2
                | -10
                | -3
                | -4
                | -5
                | -6
                | -7
                | -8
                | -9
                | -11
                | -30
                | -40
                | -18
                | -28
                | -38
                | -12
                | -13
                | -14
                | -15
                | -16
                | -17
                | -19
                | -21
                | -22
                | -23
                | -24
                | -25
                | -26
                | -27
                | -29
                | -31
                | -32
                | -33
                | -34
                | -35
                | -36
                | -37
                | -39
                | NormalizeBrandUnion<
                    number & {
                        "!=0": true;
                        Finite: true;
                        Int: true;
                        NaNValue: false;
                        SafeInt: true;
                    } & Readonly<
                        {
                            "TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3": unknown;
                        },
                    >,
                >,
        ) => SafeInt;
        fromNumber: (x: number) => SafeInt;
        is: (a: number) => a is SafeInt;
        max: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt;
        MAX_VALUE: SafeUint;
        min: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt;
        MIN_VALUE: SafeInt;
        mul: (
            x: WithSmallInt<SafeInt, 40>,
            y: WithSmallInt<SafeInt, 40>,
        ) => SafeInt;
        pow: (
            x: WithSmallInt<SafeInt, 40>,
            y: WithSmallInt<SafeInt, 40>,
        ) => SafeInt;
        random: (
            min?: WithSmallInt<SafeInt, 40>,
            max?: WithSmallInt<SafeInt, 40>,
        ) => SafeInt;
        sub: (
            x: WithSmallInt<SafeInt, 40>,
            y: WithSmallInt<SafeInt, 40>,
        ) => SafeInt;
    }

    Namespace providing type-safe operations for the SafeInt branded type.

    The SafeInt type represents a safe integer. Division (div) uses floor division.

    Type Declaration

    • Readonlyabs: (x: WithSmallInt<SafeInt, 40>) => IntersectBrand<SafeInt, NonNegativeNumber>

      Returns the absolute value of a safe integer.

      The result is non-negative and keeps the SafeInt brand.

      const negative = asSafeInt(-900);

      const absolute = SafeInt.abs(negative);

      assert.isTrue(absolute === 900);

      assert.isTrue(SafeInt.is(absolute));
    • Readonlyadd: (x: WithSmallInt<SafeInt, 40>, y: WithSmallInt<SafeInt, 40>) => SafeInt

      Adds two safe integers, returning a + b as a SafeInt.

      const sum = SafeInt.add(asSafeInt(9), asSafeInt(4));

      assert.isTrue(sum === 13);

      assert.isTrue(SafeInt.is(sum));
    • Readonlydiv: (
          x: WithSmallInt<SafeInt, 40>,
          y:
              | 20
              | 1
              | 2
              | 10
              | 3
              | 4
              | 5
              | 6
              | 7
              | 8
              | 9
              | 11
              | 30
              | 18
              | 28
              | 38
              | 12
              | 13
              | 14
              | 15
              | 16
              | 17
              | 19
              | 21
              | 22
              | 23
              | 24
              | 25
              | 26
              | 27
              | 29
              | 31
              | 32
              | 33
              | 34
              | 35
              | 36
              | 37
              | 39
              | -20
              | -1
              | -2
              | -10
              | -3
              | -4
              | -5
              | -6
              | -7
              | -8
              | -9
              | -11
              | -30
              | -40
              | -18
              | -28
              | -38
              | -12
              | -13
              | -14
              | -15
              | -16
              | -17
              | -19
              | -21
              | -22
              | -23
              | -24
              | -25
              | -26
              | -27
              | -29
              | -31
              | -32
              | -33
              | -34
              | -35
              | -36
              | -37
              | -39
              | NormalizeBrandUnion<
                  number & {
                      "!=0": true;
                      Finite: true;
                      Int: true;
                      NaNValue: false;
                      SafeInt: true;
                  } & Readonly<
                      {
                          "TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3": unknown;
                      },
                  >,
              >,
      ) => SafeInt

      Divides two safe integers using floor division (⌊a / b⌋): the result is a / b rounded toward negative infinity, as a SafeInt.

      const quotient = SafeInt.div(asSafeInt(-17), asSafeInt(5));

      assert.isTrue(quotient === -4);

      assert.isTrue(SafeInt.is(quotient));
    • ReadonlyfromNumber: (x: number) => SafeInt

      Converts an arbitrary number into a SafeInt, rounding to the nearest integer and saturating the result into the range [MIN_VALUE, MAX_VALUE].

      Unlike asSafeInt, this is total: out-of-range inputs are clamped to the nearest representable SafeInt instead of throwing.

      const aboveRange = SafeInt.fromNumber(1e20);

      const withinRange = SafeInt.fromNumber(123);

      const belowRange = SafeInt.fromNumber(-1e20);

      assert.isTrue(aboveRange === Number.MAX_SAFE_INTEGER);

      assert.isTrue(withinRange === 123);

      assert.isTrue(belowRange === Number.MIN_SAFE_INTEGER);
    • Readonlyis: (a: number) => a is SafeInt

      Type guard that checks if a value is a safe integer.

      assert.isTrue(isSafeInt(Number.MAX_SAFE_INTEGER));

      assert.isFalse(isSafeInt(Number.MAX_SAFE_INTEGER + 0.5));

      assert.isTrue(SafeInt.is(Number.MIN_SAFE_INTEGER));

      isSafeInt for usage examples

    • Readonlymax: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt

      Returns the largest of the given safe integers.

      const largest = SafeInt.max(asSafeInt(25), asSafeInt(-14), asSafeInt(99));

      assert.isTrue(largest === 99);
    • ReadonlyMAX_VALUE: SafeUint

      The largest value representable as SafeInt (the upper saturation target of fromNumber).

    • Readonlymin: (...values: readonly WithSmallInt<SafeInt, 40>[]) => SafeInt

      Returns the smallest of the given safe integers.

      const smallest = SafeInt.min(asSafeInt(25), asSafeInt(-14), asSafeInt(99));

      assert.isTrue(smallest === -14);
    • ReadonlyMIN_VALUE: SafeInt

      The smallest value representable as SafeInt (the lower saturation target of fromNumber).

    • Readonlymul: (x: WithSmallInt<SafeInt, 40>, y: WithSmallInt<SafeInt, 40>) => SafeInt

      Multiplies two safe integers, returning a * b as a SafeInt.

      const product = SafeInt.mul(asSafeInt(-8), asSafeInt(7));

      assert.isTrue(product === -56);

      assert.isTrue(SafeInt.is(product));
    • Readonlypow: (x: WithSmallInt<SafeInt, 40>, y: WithSmallInt<SafeInt, 40>) => SafeInt

      Raises a to the power b, returning a ** b as a SafeInt (floored to an integer).

      const base = asSafeInt(3);

      const exponent = asSafeInt(5);

      const power = SafeInt.pow(base, exponent);

      assert.isTrue(power === 243);

      assert.isTrue(SafeInt.is(power));
    • Readonlyrandom: (min?: WithSmallInt<SafeInt, 40>, max?: WithSmallInt<SafeInt, 40>) => SafeInt

      Generates a random SafeInt within the given range.

      The range is inclusive on both ends.

      const min = asSafeInt(-10);

      const max = asSafeInt(10);

      const randomValue = SafeInt.random(min, max);

      assert.isTrue(SafeInt.is(randomValue));

      assert.isTrue(-10 <= randomValue && randomValue <= 10);
    • Readonlysub: (x: WithSmallInt<SafeInt, 40>, y: WithSmallInt<SafeInt, 40>) => SafeInt

      Subtracts two safe integers, returning a - b as a SafeInt.

      const difference = SafeInt.sub(asSafeInt(9), asSafeInt(14));

      assert.isTrue(difference === -5);

      assert.isTrue(SafeInt.is(difference));