Skip to content

Commit

Permalink
feat: 🔖 0.4.0 - Add the IsNullable and IsNonNullable types - Check if…
Browse files Browse the repository at this point in the history
… a type is nullable or not
  • Loading branch information
CRBroughton committed Apr 26, 2024
1 parent 1e5ca20 commit 1ba9db2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ export type Position<T extends any[], U extends number> =
T extends [] ? never :
T extends any[]
? T[U] : never

export type IsNullable<T> = null extends T ? true : false

export type IsNonNullable<T> = IsNullable<T> extends true ? false : true
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
15 changes: 15 additions & 0 deletions tests/IsNonNullable.test.ts
Original file line number Diff line number Diff line change
@@ -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<IsNonNullable<{ id: number }>>
// ^?
})
it('Failed the IsNonNulable test when the type is nullable', () => {
// @ts-expect-error - Fails the IsNonNulable test
type Result = Expect<IsNonNullable<{ id: string } | null>>
// ^?
})
})
15 changes: 15 additions & 0 deletions tests/IsNullable.test.ts
Original file line number Diff line number Diff line change
@@ -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<IsNullable<{ id: number } | null>>
// ^?
})
it('Failed the IsNullable test when the type is not nullable', () => {
// @ts-expect-error - Fails the exclusion
type Result = Expect<IsNullable<{ id: string }>>
// ^?
})
})

0 comments on commit 1ba9db2

Please sign in to comment.