From e04a2d097d98cc4d33bf19f42291925dd79cc9ed Mon Sep 17 00:00:00 2001 From: Andrej Dyck <43913051+andrej-dyck@users.noreply.github.com> Date: Sat, 16 Dec 2023 16:35:56 +0100 Subject: [PATCH] add nullable types functions --- index.ts | 1 + nullables/index.ts | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 nullables/index.ts diff --git a/index.ts b/index.ts index 624306a..0792fcf 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,6 @@ export * from './json/index.js' export * from './lazy/index.js' export * from './locale/index.js' +export * from './nullables/index.js' export * from './raise/index.js' export * from './records/index.js' diff --git a/nullables/index.ts b/nullables/index.ts new file mode 100644 index 0000000..7d8c9fd --- /dev/null +++ b/nullables/index.ts @@ -0,0 +1,11 @@ +/** + * Checks whenever a value is not nullable; same as value != null + * + * Useful for Array.filter(): + * const arr: (string | undefined)[] = [...] + * ❌ arr.filter(v => v != null) // type: (string | undefined)[] + * ❌ arr.filter(v => typeof v === 'string') // type: (string | undefined)[] + * ✅ arr.filter(isNonNullable) // type: string[] + */ +export const isNonNullable = (value: NonNullable | undefined | null): value is NonNullable => + value != null