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