API reference › @evolu/common › Type › ValidateBrandFactoryBigInt
type ValidateBrandFactoryBigInt<Value extends bigint> =
IsUnion<Value> extends false
? {} extends Record<`${Value}`, never>
? Value & Readonly<Record<BrandFactoryBigIntError, never>>
: Value
: Value & Readonly<Record<BrandFactoryBigIntError, never>>;
Defined in: packages/common/src/Type.ts:6631
Bigint parameter preserving literal types in a BrandFactory.
The bigint counterpart of ValidateBrandFactoryNumber. Inline literals,
including negative ones such as -5n, and const values preserve their
literal types. Arithmetic expressions and runtime bigints widen to bigint,
while branded bigints such as Int64 can contain many runtime values.
Both are rejected so distinct constraints do not all share one broad
Brand.
Brand names built from bigint parameters should end with n, as in
GreaterThan10n, so they differ from the names of the corresponding number
constraints, which localizeTypes uses as formatter keys.
Example
import {
assertErr,
assertOk,
assertType,
BigInt,
brand,
err,
ok,
type Brand,
type BrandFactory,
type TypeError,
type ValidateBrandFactoryBigInt,
} from "@evolu/common";
interface NotEqualToBigIntError<
Excluded extends bigint,
> extends TypeError<`NotEqualTo${Excluded}n`> {
readonly value: bigint;
readonly excluded: Excluded;
}
const notEqualToBigInt =
<Excluded extends bigint>(
excluded: ValidateBrandFactoryBigInt<Excluded>,
): BrandFactory<
`NotEqualTo${Excluded}n`,
bigint,
NotEqualToBigIntError<Excluded>
> =>
(parent) => {
const name = `NotEqualTo${excluded}n` as `NotEqualTo${Excluded}n`;
return brand(
name,
parent,
(value) =>
value !== excluded
? ok()
: err<NotEqualToBigIntError<Excluded>>({
type: name,
value,
excluded,
}),
() => `Expected a bigint other than ${excluded}.`,
);
};
const NonZero = notEqualToBigInt(0n)(BigInt);
type NonZero = typeof NonZero.Output;
assertType<typeof NonZero.name, "NotEqualTo0n">();
assertType<NonZero, bigint & Brand<"NotEqualTo0n">>();
assertOk(NonZero.fromUnknown(-1n), -1n);
assertErr(NonZero.fromUnknown(0n));
// @ts-expect-error Parameter must be one concrete bigint literal instead of a widened, union, or branded bigint.
notEqualToBigInt(1n - 1n);