diff --git a/test/isNil.test.ts b/test/isNil.test.ts new file mode 100644 index 0000000..d463890 --- /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 61049da..5f67478 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 2d1d2e5..d3c51d7 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);