Skip to content

Commit b1c6950

Browse files
committed
feat: ✨ Add IsNullish type - Check if a type is either undefined or null
1 parent 02d5394 commit b1c6950

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

.changeset/heavy-rats-juggle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@crbroughton/ts-test-utils": minor
3+
---
4+
5+
Add IsNullish type - Check if a type is either undefined or null

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ A collection of helper TypeScript types to test other TypeScript types. This col
1616
- IsNonNullable - Check if a type is not nullable
1717
- IsUndefined - Check if a type is undefined
1818
- IsNonUndefined - Check if a type is not undefined
19+
- IsNullish - Check if a type is either undefined or null
1920

2021
## Installation
2122

index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ export type IsNonNullable<T> = IsNullable<T> extends true ? false : true
3030
export type IsUndefined<T> = undefined extends T ? true : false
3131

3232
export type IsNonUndefined<T> = IsUndefined<T> extends true ? false : true
33+
34+
export type IsNullish<T> =
35+
IsNullable<T> extends true ?
36+
true
37+
: IsUndefined<T> extends true ?
38+
true : false

tests/IsNullish.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable unused-imports/no-unused-vars */
2+
import { describe, it } from 'bun:test'
3+
import type { Expect, IsNullish } from '../index'
4+
5+
describe('IsNullable tests', () => {
6+
it('Passes the IsNullable test when the type is nullable', () => {
7+
type ResultNull = Expect<IsNullish<{ id: number } | null>>
8+
// ^?
9+
type ResultUndefined = Expect<IsNullish<{ id: number } | undefined>>
10+
// ^?
11+
type ResultBoth = Expect<IsNullish<{ id: number } | null | undefined>>
12+
// ^?
13+
})
14+
it('Failed the IsNullable test when the type is not nullable', () => {
15+
// @ts-expect-error - Fails the exclusion
16+
type Result = Expect<IsNullish<{ id: string }>>
17+
// ^?
18+
})
19+
})

0 commit comments

Comments
 (0)