From 3e11f026360d2980f18c5d7e0392b3b30bf1188e Mon Sep 17 00:00:00 2001 From: CRBroughton Date: Sat, 27 Apr 2024 19:53:25 +0100 Subject: [PATCH] feat: :sparkles: Add IsNonNullish type - Check if a type is neither undefined or null --- .changeset/cyan-ducks-shout.md | 5 +++++ README.md | 2 +- index.ts | 6 ++++++ tests/IsNonNullish.test.ts | 25 +++++++++++++++++++++++++ tests/IsNullish.test.ts | 6 +++--- 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 .changeset/cyan-ducks-shout.md create mode 100644 tests/IsNonNullish.test.ts diff --git a/.changeset/cyan-ducks-shout.md b/.changeset/cyan-ducks-shout.md new file mode 100644 index 0000000..5ea69f3 --- /dev/null +++ b/.changeset/cyan-ducks-shout.md @@ -0,0 +1,5 @@ +--- +"@crbroughton/ts-test-utils": minor +--- + +Add IsNonNullish type - Check if a type is neither undefined or null diff --git a/README.md b/README.md index 63b7443..00f72ea 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ A collection of helper TypeScript types to test other TypeScript types. This col - IsUndefined - Check if a type is undefined - IsNonUndefined - Check if a type is not undefined - IsNullish - Check if a type is either undefined or null - +- IsNonNullish - Check if a type is neither undefined or null ## Installation To install `ts-test-utils` with Bun, run the following command: diff --git a/index.ts b/index.ts index 3e0d235..b45bfcf 100644 --- a/index.ts +++ b/index.ts @@ -36,3 +36,9 @@ export type IsNullish = true : IsUndefined extends true ? true : false + +export type IsNonNullish = + IsNullable extends true ? + false + : IsUndefined extends true ? + false : true diff --git a/tests/IsNonNullish.test.ts b/tests/IsNonNullish.test.ts new file mode 100644 index 0000000..4505e78 --- /dev/null +++ b/tests/IsNonNullish.test.ts @@ -0,0 +1,25 @@ +/* eslint-disable unused-imports/no-unused-vars */ +import { describe, it } from 'bun:test' +import type { Expect, IsNonNullish } from '../index' + +describe('IsNonNullish tests', () => { + it('Passes the IsNonNullish test when the type is not nullish', () => { + type ResultNull = Expect> + // ^? + type ResultUndefined = Expect> + // ^? + type ResultBoth = Expect> + // ^? + }) + it('Failed the IsNonNullish test when the type is nullish', () => { + // @ts-expect-error - Fails the exclusion + type ResultNull = Expect> + // ^? + // @ts-expect-error - Fails the exclusion + type ResultUndefined = Expect> + // ^? + // @ts-expect-error - Fails the exclusion + type ResultBoth = Expect> + // ^? + }) +}) diff --git a/tests/IsNullish.test.ts b/tests/IsNullish.test.ts index e569aca..9927df4 100644 --- a/tests/IsNullish.test.ts +++ b/tests/IsNullish.test.ts @@ -2,8 +2,8 @@ import { describe, it } from 'bun:test' import type { Expect, IsNullish } from '../index' -describe('IsNullable tests', () => { - it('Passes the IsNullable test when the type is nullable', () => { +describe('IsNullish tests', () => { + it('Passes the IsNullish test when the type is nullish', () => { type ResultNull = Expect> // ^? type ResultUndefined = Expect> @@ -11,7 +11,7 @@ describe('IsNullable tests', () => { type ResultBoth = Expect> // ^? }) - it('Failed the IsNullable test when the type is not nullable', () => { + it('Failed the IsNullish test when the type is not nullish', () => { // @ts-expect-error - Fails the exclusion type Result = Expect> // ^?