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>> + // ^? + }) +})