From de93f43c1cb2cb9e40f6def17cde171f4b71c16e Mon Sep 17 00:00:00 2001 From: harris-miller Date: Sat, 27 Jan 2024 18:24:27 -0700 Subject: [PATCH] fix and added tests --- test/isNil.test.ts | 26 ++++++++++++++++++++++++++ test/isNotNil.test.ts | 4 +++- types/isNil.d.ts | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/isNil.test.ts diff --git a/test/isNil.test.ts b/test/isNil.test.ts new file mode 100644 index 00000000..d463890d --- /dev/null +++ b/test/isNil.test.ts @@ -0,0 +1,26 @@ +import { expectType } from 'tsd'; +import { filter, isNil } from '../es'; + +expectType(isNil(1)); +expectType(isNil('a')); +expectType(isNil(true)); + +expectType(isNil(null)); +expectType(isNil(undefined)); + +const maybeNumber = (): number | null => { + if (Math.random() > 0.5) { + return null; + } + return 123; +}; + +expectType(maybeNumber()); + +const r = maybeNumber(); + +if (isNil(r)) { + expectType(r); +} + +expectType(filter(isNil, [] as (string | undefined)[])); diff --git a/test/isNotNil.test.ts b/test/isNotNil.test.ts index 61049daf..5f674786 100644 --- a/test/isNotNil.test.ts +++ b/test/isNotNil.test.ts @@ -1,5 +1,5 @@ import { expectType } from 'tsd'; -import { isNotNil } from '../es'; +import { filter, isNotNil } from '../es'; expectType(isNotNil(1)); expectType(isNotNil('a')); @@ -22,3 +22,5 @@ const r = maybeNumber(); if (isNotNil(r)) { expectType(r); } + +expectType(filter(isNotNil, [] as (string | undefined)[])); diff --git a/types/isNil.d.ts b/types/isNil.d.ts index 2d1d2e57..d3c51d70 100644 --- a/types/isNil.d.ts +++ b/types/isNil.d.ts @@ -1 +1 @@ -export function isNil(value: any): value is null | undefined; +export function isNil(value: T): value is ((null | undefined) & T);