Skip to content

Commit 1ba9db2

Browse files
committed
feat: 🔖 0.4.0 - Add the IsNullable and IsNonNullable types - Check if a type is nullable or not
1 parent 1e5ca20 commit 1ba9db2

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @crbroughton/ts-test-utils
22

3+
## 0.4.0
4+
5+
### Minor Changes
6+
7+
- Add the IsNullable and IsNonNullable types - Check if a type is nullable or not
8+
39
## 0.3.0
410

511
### Minor Changes

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ A collection of helper TypeScript types to test other TypeScript types. This col
1212
- isNotArray - Checks if a type is not an array
1313
- Length - Check a given types length; Combine this with the 'Equals' type checker
1414
- Position - Returns a type in the given position of an array; Combine this with the 'Equals' type checker
15+
- IsNullable - Check if a type is nullable
16+
- IsNonNullable - Check if a type is not nullable
1517

1618
## Installation
1719

index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ export type Position<T extends any[], U extends number> =
2222
T extends [] ? never :
2323
T extends any[]
2424
? T[U] : never
25+
26+
export type IsNullable<T> = null extends T ? true : false
27+
28+
export type IsNonNullable<T> = IsNullable<T> extends true ? false : true

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@crbroughton/ts-test-utils",
33
"type": "module",
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"description": "A collection of testing helper types",
66
"author": "Craig R Broughton",
77
"license": "MIT",

tests/IsNonNullable.test.ts

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

tests/IsNullable.test.ts

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

0 commit comments

Comments
 (0)