isNotUndefined function
Checks whether a value is not undefined; null passes, unlike with isDefined.
ts
import { isNotUndefined } from 'utils';Signature
ts
export function isNotUndefined<T>(value: T): value is Exclude<T, undefined>Parameters
| Name | Description |
|---|---|
T | The type of the value. |
value | The value to check. |
Returns
true for anything but undefined.
Example
ts
[1, null, undefined].filter(isNotUndefined); // [1, null], typed (number | null)[]Tests
Line coverage: 100 %.
- ✅ excludes undefined only
Source
src/guard/is-not-undefined.ts (on GitHub)
Source code
ts
/**
* Checks whether a value is not `undefined`; `null` passes, unlike with `isDefined`.
*
* @template T - The type of the value.
* @param value - The value to check.
* @returns `true` for anything but `undefined`.
* @example
* [1, null, undefined].filter(isNotUndefined); // [1, null], typed (number | null)[]
*/
export function isNotUndefined<T>(value: T): value is Exclude<T, undefined> {
return value !== undefined;
}Test file
ts
import { isNotUndefined } from './is-not-undefined';
describe(isNotUndefined, () => {
it('excludes undefined only', () => {
expect([0, null, undefined].filter(isNotUndefined)).toStrictEqual([0, null]);
});
});Keywords: is, not, undefined, guards, function