<const R extends { readonly [x: string]: Order<any> }>(fields: R): Order<{
[K in keyof R]: [R[K]] extends [Order<infer A>] ? A : never
}>Creates an Order for structs by applying the given Orders to each property in sequence.
When to use
Use when you need multi-field ordering for objects with known properties.
Details
Compares structs field-by-field in the key order of the fields object and
stops at the first non-zero comparison result. Field order matters: earlier
fields take precedence. The result is 0 only if all fields are equal.
Example (Ordering structs)
import { Order } from "effect"
const personOrder = Order.Struct({
name: Order.String,
age: Order.Number
})
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
const person3 = { name: "Alice", age: 25 }
console.log(personOrder(person1, person2)) // -1 (Alice < Bob)
console.log(personOrder(person1, person3)) // 1 (same name, 30 > 25)
console.log(personOrder(person1, person1)) // 0export function function Struct<
R extends { readonly [x: string]: Order<any> }
>(
fields: R
): Order<{
[K in keyof R]: [R[K]] extends [Order<infer A>]
? A
: never
}>
Creates an Order for structs by applying the given Orders to each property in sequence.
When to use
Use when you need multi-field ordering for objects with known properties.
Details
Compares structs field-by-field in the key order of the fields object and
stops at the first non-zero comparison result. Field order matters: earlier
fields take precedence. The result is 0 only if all fields are equal.
Example (Ordering structs)
import { Order } from "effect"
const personOrder = Order.Struct({
name: Order.String,
age: Order.Number
})
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
const person3 = { name: "Alice", age: 25 }
console.log(personOrder(person1, person2)) // -1 (Alice < Bob)
console.log(personOrder(person1, person3)) // 1 (same name, 30 > 25)
console.log(personOrder(person1, person1)) // 0
Struct<const function (type parameter) R in Struct<const R extends {
readonly [x: string]: Order<any>;
}>(fields: R): Order<{ [K in keyof R]: [R[K]] extends [Order<infer A>] ? A : never; }>
R extends { readonly [x: stringx: string]: interface Order<in A>Represents a total ordering for values of type A.
When to use
Use when you need to define how values of a type are compared.
Details
An order returns -1 when the first value is less than the second, 0 when
the values are equal according to this ordering, and 1 when the first value
is greater than the second. It must satisfy total ordering laws: totality,
antisymmetry, and transitivity.
Example (Defining a custom Order)
import { Order } from "effect"
const byAge: Order.Order<{ name: string; age: number }> = (self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
}
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
console.log(byAge(person1, person2)) // 1
Order<any> }>(
fields: const R extends { readonly [x: string]: Order<any>; }fields: function (type parameter) R in Struct<const R extends {
readonly [x: string]: Order<any>;
}>(fields: R): Order<{ [K in keyof R]: [R[K]] extends [Order<infer A>] ? A : never; }>
R
): interface Order<in A>Represents a total ordering for values of type A.
When to use
Use when you need to define how values of a type are compared.
Details
An order returns -1 when the first value is less than the second, 0 when
the values are equal according to this ordering, and 1 when the first value
is greater than the second. It must satisfy total ordering laws: totality,
antisymmetry, and transitivity.
Example (Defining a custom Order)
import { Order } from "effect"
const byAge: Order.Order<{ name: string; age: number }> = (self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
}
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
console.log(byAge(person1, person2)) // 1
Order<{ [function (type parameter) KK in keyof function (type parameter) R in Struct<const R extends {
readonly [x: string]: Order<any>;
}>(fields: R): Order<{ [K in keyof R]: [R[K]] extends [Order<infer A>] ? A : never; }>
R]: [function (type parameter) R in Struct<const R extends {
readonly [x: string]: Order<any>;
}>(fields: R): Order<{ [K in keyof R]: [R[K]] extends [Order<infer A>] ? A : never; }>
R[function (type parameter) KK]] extends [interface Order<in A>Represents a total ordering for values of type A.
When to use
Use when you need to define how values of a type are compared.
Details
An order returns -1 when the first value is less than the second, 0 when
the values are equal according to this ordering, and 1 when the first value
is greater than the second. It must satisfy total ordering laws: totality,
antisymmetry, and transitivity.
Example (Defining a custom Order)
import { Order } from "effect"
const byAge: Order.Order<{ name: string; age: number }> = (self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
}
const person1 = { name: "Alice", age: 30 }
const person2 = { name: "Bob", age: 25 }
console.log(byAge(person1, person2)) // 1
Order<infer function (type parameter) AA>] ? function (type parameter) AA : never }> {
const const keys: string[]keys = var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.keys(o: {}): string[] (+1 overload)Returns the names of the enumerable string properties and methods of an object.
keys(fields: const R extends { readonly [x: string]: Order<any>; }fields)
return function make<A>(
compare: (self: A, that: A) => -1 | 0 | 1
): Order<A>
Creates a new Order instance from a comparison function.
When to use
Use when you need a sorting rule not covered by the built-in orders or input
mapping helpers, and you can provide a total comparison.
Details
Uses reference equality (===) as a shortcut: if self === that, it returns
0 without calling the comparison function. The comparison function should
return -1, 0, or 1, and the returned order satisfies total ordering
laws when the comparison function does.
Example (Creating an Order)
import { Order } from "effect"
const byAge = Order.make<{ name: string; age: number }>((self, that) => {
if (self.age < that.age) return -1
if (self.age > that.age) return 1
return 0
})
console.log(byAge({ name: "Alice", age: 30 }, { name: "Bob", age: 25 })) // 1
console.log(byAge({ name: "Alice", age: 25 }, { name: "Bob", age: 30 })) // -1
make((self: {
[K in keyof R]: [R[K]] extends [Order<infer A>]
? A
: never
}
self, that: {
[K in keyof R]: [R[K]] extends [Order<infer A>]
? A
: never
}
that) => {
for (const const key: stringkey of const keys: string[]keys) {
const const o: Orderingo = fields: const R extends { readonly [x: string]: Order<any>; }fields[const key: stringkey](self: {
[K in keyof R]: [R[K]] extends [Order<infer A>]
? A
: never
}
self[const key: stringkey], that: {
[K in keyof R]: [R[K]] extends [Order<infer A>]
? A
: never
}
that[const key: stringkey])
if (const o: Orderingo !== 0) {
return const o: 1 | -1o
}
}
return 0
})
}