New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@technically/is-not-null

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-null

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

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@technically/is-not-null

Test

@technically/is-not-null is a micro-package to check if a value is not null. With proper TS typing!

Usage

import { isNotNull } from '@technically/is-not-null';

const values = ['hello', 'world', null];

const uppercase = values.filter(isNotNull).map((value) => {
  return value.toUpperCase(); // OK
});

Why

In Typescript, it is often needed to filter out nulls 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', null];

const uppercase = values.filter(Boolean).map((value) => {
  return value.toUpperCase(); // TS18047: 'value' is possibly 'null'.
});

To mitigate the issue you have to wrap Boolean into a type-guard function like this:

const values = ['hello', 'world', null];

const uppercase = values.filter((x): x is Exclude<typeof x, null>).map((value) => {
  return value.toUpperCase(); // TS18047: 'value' is possibly 'null'.
});

To avoid writing these ad-hoc type-guards every time, I've created this package.

License

MIT

Credits

Authored by Ivan Voskoboinyk.

Keywords

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc