New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@technically/is-not-undefined

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@technically/is-not-undefined

A micro-package to check if a value is not `undefined`. With proper TS typing!

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
437
14.7%
Maintainers
1
Weekly downloads
 
Created
Source

@technically/is-not-undefined

Test

@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.

License

MIT

Credits

Authored by Ivan Voskoboinyk.

Keywords

is-not-undefined

FAQs

Package last updated on 12 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts