Skip to content

Commit

Permalink
feat: ✨ Add IsNonNullish type - Check if a type is neither undefined …
Browse files Browse the repository at this point in the history
…or null
  • Loading branch information
CRBroughton committed Apr 27, 2024
1 parent b1c6950 commit 3e11f02
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-ducks-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crbroughton/ts-test-utils": minor
---

Add IsNonNullish type - Check if a type is neither undefined or null
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ export type IsNullish<T> =
true
: IsUndefined<T> extends true ?
true : false

export type IsNonNullish<T> =
IsNullable<T> extends true ?
false
: IsUndefined<T> extends true ?
false : true
25 changes: 25 additions & 0 deletions tests/IsNonNullish.test.ts
Original file line number Diff line number Diff line change
@@ -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<IsNonNullish<{ id: number }>>
// ^?
type ResultUndefined = Expect<IsNonNullish<{ id: number }>>
// ^?
type ResultBoth = Expect<IsNonNullish<{ id: number }>>
// ^?
})
it('Failed the IsNonNullish test when the type is nullish', () => {
// @ts-expect-error - Fails the exclusion
type ResultNull = Expect<IsNonNullish<{ id: number } | null>>
// ^?
// @ts-expect-error - Fails the exclusion
type ResultUndefined = Expect<IsNonNullish<{ id: number } | undefined>>
// ^?
// @ts-expect-error - Fails the exclusion
type ResultBoth = Expect<IsNonNullish<{ id: number } | null | undefined>>
// ^?
})
})
6 changes: 3 additions & 3 deletions tests/IsNullish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
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<IsNullish<{ id: number } | null>>
// ^?
type ResultUndefined = Expect<IsNullish<{ id: number } | undefined>>
// ^?
type ResultBoth = Expect<IsNullish<{ id: number } | null | undefined>>
// ^?
})
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<IsNullish<{ id: string }>>
// ^?
Expand Down

0 comments on commit 3e11f02

Please sign in to comment.