Skip to content

Primitives ​

Business primitives are an alternative to raw TypeScript string and number.

Rather than manipulating bare values, each primitive is wrapped in a container. Result: safer, more explicit data, better aligned with the domain.

Example ​

a selected page is not a simple number, but a dedicated primitive, independent of any business entity.

Primitives have two goals:

Secure data by avoiding the use of raw values.

Unify types: when several NewTypes interact, their common point falls back to a simple primitive (number, string, etc.).

👉 In short: cleaner, more readable, and more robust types, without unnecessary complexity

Available primitives ​

Methods and Properties ​

Methods ​

create() ​

typescript
function create(
	value: RawType
): Right<Primitive<RawType>> | Left<C.PrimitiveError<PrimitiveName>>

createOrThrow() ​

typescript
function createOrThrow(
	value: RawType
): Primitive<RawType>

createWithLarge() ​

typescript
function createWithLarge(
	value: LargeInput
): Right<Primitive<RawType>> | Left<C.PrimitiveError<PrimitiveName>>

Uses the wider input type carried by the handler. If no wider input type exists, the method accepts the initial primitive type.

createWithLargeOrThrow() ​

typescript
function createWithLargeOrThrow(
	value: LargeInput
): Primitive<RawType>

Throws C.CreatePrimitiveError if validation fails.

createWithUnknown() ​

typescript
function createWithUnknown(
	value: unknown
): Right<Primitive<RawType>> | Left<C.PrimitiveError<PrimitiveName>>

createWithUnknownOrThrow() ​

typescript
function createWithUnknownOrThrow(
	value: unknown
): Primitive<RawType>

is() ​

typescript
function is(
	value: unknown
): value is Primitive<RawType>

Operators ​

equal ​

Compares two wrapped primitives (or a primitive and a raw value) with a type guard.

matchWithString ​

Performs exhaustive matching with raw string keys and passes the narrowed original Clean primitive.

matchWithStringOtherwise ​

Partially matches raw string keys and passes remaining wrapped values to a typed fallback.

matchWithNumber ​

Performs exhaustive matching with raw number keys and passes the narrowed original Clean primitive.

matchWithNumberOtherwise ​

Partially matches raw number keys and passes remaining wrapped values to a typed fallback.

add ​

Adds two Number (supports the curried version).

subtract ​

Subtracts a number from a Number (supports the curried version).

multiply ​

Multiplies a Number (supports the curried version).

divide ​

Divides a Number (supports the curried version).

min ​

Returns the minimum of a Number tuple.

max ​

Returns the maximum of a Number tuple.

greaterThan ​

Checks whether a Number is strictly greater than a threshold.

lessThan ​

Checks whether a Number is strictly less than a threshold.

concat ​

Concatenates String (supports the curried version).

length ​

Returns the length of a String as a Number.

lengthEqual ​

Checks whether the length of a String equals a value.

lengthGreaterThan ​

Checks whether the length of a String is greater than a value.

lengthLessThan ​

Checks whether the length of a String is less than a value.

dateGreaterThan ​

Checks whether a Date is after a threshold.

dateLessThan ​

Checks whether a Date is before a threshold.

dateAddTime ​

Adds a duration to a Date.

dateSubtractTime ​

Subtracts a duration from a Date.

dateMin ​

Returns the smallest date in a list.

dateMax ​

Returns the largest date in a list.

timeGreaterThan ​

Checks whether a Time is strictly greater than a threshold.

timeLessThan ​

Checks whether a Time is strictly less than a threshold.

timeMin ​

Returns the smallest duration in a list.

timeMax ​

Returns the largest duration in a list.

sort ​

Sorts an array of primitives (String, Number, Date, or Time) in "ASC" / "DSC".

Released under the MIT license.