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
): EitherRight<Primitive<RawType>> | EitherLeft<ParseError>

createOrThrow()

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

createWithUnknown()

typescript
function createWithUnknown(
	value: unknown
): EitherRight<Primitive<RawType>> | EitherLeft<ParseError>

createWithUnknownOrThrow()

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

is()

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

Properties

dataParser

Corresponds to the dataParser that validates the primitive.

Operators

equal

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

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.