Skip to content

Commit

Permalink
feat: ✨ Add the NotEquals type - Checks if a type does not equal anot…
Browse files Browse the repository at this point in the history
…her type
  • Loading branch information
CRBroughton committed Apr 17, 2024
1 parent a334fde commit c5bb362
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-needles-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crbroughton/ts-test-utils": minor
---

Add the NotEquals type - Checks if a type does not equal another type
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type Expect<T extends true> = T

export type Equals<T, U> = T extends U ? U extends T ? true : false : false

export type NotEquals<X, Y> = true extends Equals<X, Y> ? false : true

export type Includes<T extends U, U> = T extends U ? true : false

export type Excludes<T, U> = [T] extends [U] ? false : true
Expand Down
20 changes: 20 additions & 0 deletions tests/NotEquals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable unused-imports/no-unused-vars */
import { describe, it } from 'bun:test'
import type { Expect, NotEquals } from '../index'

describe('NotEquals tests', () => {
it('Passes the equality test when the types are not equal', () => {
type ResultBoolean = Expect<NotEquals<true, false>>
// ^?
type ResultRecord = Expect<NotEquals<{ id: number }, { id: string }>>
// ^?
})
it('Fails the equality test when the types are equal', () => {
// @ts-expect-error - Boolean values failing the equality checker
type ResultBoolean = Expect<NotEquals<true, true>>
// ^?
// @ts-expect-error - Object / Record failing the equality checker
type ResultRecord = Expect<NotEquals<{ id: number }, { id: number }>>
// ^?
})
})

0 comments on commit c5bb362

Please sign in to comment.