export interface Eq<A> {
readonly equals: (x: A, y: A) => boolean
}
The Eq
type class represents types which support decidable equality.
Instances must satisfy the following laws:
- Reflexivity:
E.equals(a, a) === true
- Symmetry:
E.equals(a, b) === E.equals(b, a)
- Transitivity: if
E.equals(a, b) === true
andE.equals(b, c) === true
, thenE.equals(a, c) === true
Example
import { Eq } from 'fp-ts/Eq'
export const string: Eq<string> = {
equals: (x, y) => x === y
}
string: Eq<string>
number: Eq<number>
boolean: Eq<boolean>
UnknownArray: Eq<Array<unknown>>
UnknownRecord: Eq<Record<string, unknown>>
literal
nullable
type
partial
record
array
tuple
intersection
sum
lazy
Example
import * as E from 'io-ts/Eq'
const Person = E.type({
name: E.string,
age: E.number
})
console.log(Person.equals({ name: 'a', age: 0 }, { name: 'a', age: 0 })) // => true
console.log(Person.equals({ name: 'a', age: 0 }, { name: '', age: 0 })) // => false
console.log(Person.equals({ name: 'a', age: 0 }, { name: 'a', age: 1 })) // => false