From a1ca1ff81572fa7f50380186ac324ef24a4eaf54 Mon Sep 17 00:00:00 2001 From: CRBroughton Date: Sun, 21 Apr 2024 16:39:44 +0100 Subject: [PATCH] feat: :sparkles: Add the Length type - Check a given types length --- .changeset/red-apples-grow.md | 5 +++++ index.ts | 2 ++ tests/Length.test.ts | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 .changeset/red-apples-grow.md create mode 100644 tests/Length.test.ts diff --git a/.changeset/red-apples-grow.md b/.changeset/red-apples-grow.md new file mode 100644 index 0000000..8dfb121 --- /dev/null +++ b/.changeset/red-apples-grow.md @@ -0,0 +1,5 @@ +--- +"@crbroughton/ts-test-utils": minor +--- + +Add the Length type - Check a given types length diff --git a/index.ts b/index.ts index e7465b5..7a5fd97 100644 --- a/index.ts +++ b/index.ts @@ -15,3 +15,5 @@ export type Extends = U extends T ? true : false export type isArray = T extends any[] ? true : false export type isNonArray = isArray extends true ? false : true + +export type Length = T['length'] diff --git a/tests/Length.test.ts b/tests/Length.test.ts new file mode 100644 index 0000000..d998df5 --- /dev/null +++ b/tests/Length.test.ts @@ -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, 3>> + // ^? + type ResultRecord = Expect, 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, 10>> + // ^? + // @ts-expect-error - Object / Record failing the length checker + type ResultRecord = Expect, 10>> + // ^? + }) +})