@technically/is-not-undefined
v1.0.0
Published
A micro-package to check if a value is not `undefined`. With proper TS typing!
Downloads
1,082
Maintainers
Readme
@technically/is-not-undefined
@technically/is-not-undefined
is a micro-package to check if a value is not null. With proper TS typing!
Usage
import { isNotUndefined } from '@technically/is-not-undefined';
const values = ['hello', 'world', undefined];
const uppercase = values.filter(isNotUndefined).map((value) => {
return value.toUpperCase(); // OK
});
Why
In Typescript, it is often needed to filter out undefined
from a list of values, and then process them.
The problem is that Boolean
does not currently work as a type-guard in Typescript:
const values = ['hello', 'world', undefined];
const uppercase = values.filter(Boolean).map((value) => {
return value.toUpperCase(); // TS18048: 'value' is possibly 'undefined'.
});
To mitigate the issue you have to wrap Boolean
into a type-guard function like this:
const values = ['hello', 'world', undefined];
const uppercase = values.filter((x): x is Exclude<typeof x, undefined> => Boolean(x)).map((value) => {
return value.toUpperCase(); // OK
});
To avoid writing these ad-hoc type-guards every time, I've created this package.
Related packages
License
Credits
Authored by Ivan Voskoboinyk.