Skip to content

Conversation

@ahrjarrett
Copy link
Member

With this PR, users can import Eq from @traversable/derive-equals and use Eq.fromSchema to get an "equals function" for free.

const Todo = t.object({
  complete: t.boolean,
  title: t.string,
  body: t.optional(t.array(t.string)),
})
interface Todo extends t.typeof<typeof Todo> {}
const todoEquals = Eq.fromSchema(Todo) 
//     ^? const todoEquals: Eq<{ complete: boolean, title: string, body?: string[] }>
const A: Todo = {
  complete: false,
  title: 'Todo A',
  body: [
    'Line 1',
    'Line 2',
    'Line 3',
  ]
}
const B: Todo = {
  ...A,
  body: [
    ...A.body,
  ]
}
const C: Todo = {
  ...A,
  body: [
    ...A.body,
    'Line 4',
  ]
}

// success:
console.log(todoEquals(A, B)) // => true
console.log(todoEquals(A, A)) // => true
console.log(todoEquals(B, B)) // => true

// failure:
console.log(todoEquals(A, C)) // => false
console.log(todoEquals(B, A)) // => false

@ahrjarrett ahrjarrett merged commit fdccb94 into main Mar 2, 2025
2 checks passed
@ahrjarrett ahrjarrett deleted the equals branch March 2, 2025 03:07
@github-actions github-actions bot mentioned this pull request Mar 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants