diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a26786..ab08939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @crbroughton/ts-test-utils +## 0.4.0 + +### Minor Changes + +- Add the IsNullable and IsNonNullable types - Check if a type is nullable or not + ## 0.3.0 ### Minor Changes diff --git a/README.md b/README.md index 6e7c816..cd50d13 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ A collection of helper TypeScript types to test other TypeScript types. This col - isNotArray - Checks if a type is not an array - Length - Check a given types length; Combine this with the 'Equals' type checker - Position - Returns a type in the given position of an array; Combine this with the 'Equals' type checker +- IsNullable - Check if a type is nullable +- IsNonNullable - Check if a type is not nullable ## Installation diff --git a/index.ts b/index.ts index 333f5c0..6c961a8 100644 --- a/index.ts +++ b/index.ts @@ -22,3 +22,7 @@ export type Position = T extends [] ? never : T extends any[] ? T[U] : never + +export type IsNullable = null extends T ? true : false + +export type IsNonNullable = IsNullable extends true ? false : true diff --git a/package.json b/package.json index 2b20414..b566bbf 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@crbroughton/ts-test-utils", "type": "module", - "version": "0.3.0", + "version": "0.4.0", "description": "A collection of testing helper types", "author": "Craig R Broughton", "license": "MIT", diff --git a/tests/IsNonNullable.test.ts b/tests/IsNonNullable.test.ts new file mode 100644 index 0000000..5e31bf1 --- /dev/null +++ b/tests/IsNonNullable.test.ts @@ -0,0 +1,15 @@ +/* eslint-disable unused-imports/no-unused-vars */ +import { describe, it } from 'bun:test' +import type { Expect, IsNonNullable } from '../index' + +describe('IsNonNulable tests', () => { + it('Passes the IsNonNulable test when the type is not nullable', () => { + type Result = Expect> + // ^? + }) + it('Failed the IsNonNulable test when the type is nullable', () => { + // @ts-expect-error - Fails the IsNonNulable test + type Result = Expect> + // ^? + }) +}) diff --git a/tests/IsNullable.test.ts b/tests/IsNullable.test.ts new file mode 100644 index 0000000..0ff42ae --- /dev/null +++ b/tests/IsNullable.test.ts @@ -0,0 +1,15 @@ +/* eslint-disable unused-imports/no-unused-vars */ +import { describe, it } from 'bun:test' +import type { Expect, IsNullable } from '../index' + +describe('IsNullable tests', () => { + it('Passes the IsNullable test when the type is nullable', () => { + type Result = Expect> + // ^? + }) + it('Failed the IsNullable test when the type is not nullable', () => { + // @ts-expect-error - Fails the exclusion + type Result = Expect> + // ^? + }) +})