Skip to content

Commit

Permalink
feat: ✨ Add the Length type - Check a given types length
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBroughton committed Apr 21, 2024
1 parent 33c7a46 commit a1ca1ff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-apples-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crbroughton/ts-test-utils": minor
---

Add the Length type - Check a given types length
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export type Extends<T, U> = U extends T ? true : false
export type isArray<T> = T extends any[] ? true : false

export type isNonArray<T> = isArray<T> extends true ? false : true

export type Length<T extends readonly any[]> = T['length']
20 changes: 20 additions & 0 deletions tests/Length.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 { Equals, Expect, Length } from '../index'

describe('Length tests', () => {
it('Passes the length test when the lengths dont match', () => {
type Result = Expect<Equals<Length<[0, 1, 2]>, 3>>
// ^?
type ResultRecord = Expect<Equals<Length<[{ id: string }, { name: string }]>, 2>>
// ^?
})
it('Fails the length test when the types are not of the same length', () => {
// @ts-expect-error - number values failing the length checker
type Results = Expect<Equals<Length<[0, 1, 2]>, 10>>
// ^?
// @ts-expect-error - Object / Record failing the length checker
type ResultRecord = Expect<Equals<Length<[{ id: string }, { name: string }]>, 10>>
// ^?
})
})

0 comments on commit a1ca1ff

Please sign in to comment.