npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typescript-nullable

v0.6.0

Published

A TypeScript Nullable<T> Type and Monad Compliant Utility Functions

Downloads

2,410

Readme

TypeScript Nullable

npm version CircleCI codecov

What is This?

Glad you asked. This is a type-safe formalization of the concept of possibly absent values in TypeScript. It is perhaps even more importantly a module of type-safe utility functions that deal with possibly absent values.

Think of it roughly like a JavaScript-friendly version of Haskell’s or Elm’s Maybe type and corresponding module of functions for dealing with Maybe. It is functional to its core, with typed and curried pure functions.

Installation

From the command line:

$ npm install --save typescript-nullable

In your TypeScript files:

import { Nullable } from 'typescript-nullable'

Nullable Type Definition

type None = null | undefined

type Nullable<T> = T | None

Module Utility Functions

This module also ships with a Nullable object that contains multiple useful functions for dealing with potentially absent values. Thus we have both a Nullable type and a Nullable object of utility functions.

All utility functions are curried to the extent that their final argument is optional. If a final argument is not provided, the function will return another function that expects that final argument.

Nullable.isNone

Determines if a provided Nullable is None and provides a type guard.

Type Annotation
<T>(nullable: Nullable<T>): nullable is None
Example Usage
Nullable.isNone('noob noob') // false
Nullable.isNone(null) // true
Nullable.isNone(undefined) // true

const possiblyNullValue: Nullable<string> = 'noob noob'

if (Nullable.isNone(possiblyNullValue)) {
  // in this scope, TypeScript knows possiblyNullValue is a None
}

Nullable.isSome

Determines if a provided Nullable is a concrete value and provides a type guard.

Type Annotation
<T>(nullable: Nullable<T>): nullable is T
Example Usage
Nullable.isNone('noob noob') // true
Nullable.isNone(null) // false
Nullable.isNone(undefined) // false

const possiblyNullValue: Nullable<string> = 'noob noob'

if (Nullable.isSome(possiblyNullValue)) {
  // in this scope, TypeScript knows possiblyNullValue is a concrete string
}

Nullable.map

Applies the provided function to the provided Nullable only if it is not None. Returns null otherwise.

Type Annotation
<A, B>(func: (val: A) => B): (nullable: Nullable<A>) => Nullable<B>
Example Usage
const toUpper = (text: string): string => text.toUpperCase()
Nullable.map(toUpper, 'noob noob') // NOOB NOOB
Nullable.map(toUpper, null) // null
Nullable.map(toUpper, undefined) // null

Nullable.withDefault

Provided a default value and a Nullable, will return the default value when the Nullable is None. Will return the concrete value of the Nullable if it is, in fact, concrete.

Type Annotation
<T>(defaultVal: T): (nullable: Nullable<T>) => T
Example Usage
Nullable.withDefault('morty')('rick') // 'rick'
Nullable.withDefault('morty')(null) // 'morty'

Nullable.maybe

Provided a default value, a function, and a Nullable, will return the default value when the Nullable is None. Will return the provided function applied to the concrete value of the Nullable if it is, in fact, concrete.

Type Annotation
<A, B>(defaultVal: B, f: (a: A) => B): (nullable: Nullable<A>) => B
Example Usage
import { add } from 'ramda'
Nullable.maybe(7, add(83), null)) // 7
Nullable.maybe(7, add(83), 34)) // 117

Nullable.andThen

Used for chaining functions that take a raw value of type T but return a Nullable<T>. This is like Haskell's bind or >>=.

Type Annotation
Nullable.andThen<A, B>(func: (val: A) => Nullable<B>): (nullable: Nullable<A>) => Nullable<B>
Example Usage
import { compose, curry } from 'ramda'

// Some arbitrary function that returns a Nullable:
const safeDivide = curry((a: number, b: number): Nullable<number> => {
  return a === 0
    ? null
    : b / a
})

compose(
  Nullable.andThen(safeDivide(3)),
  Nullable.andThen(safeDivide(0)), // this line results in a None value so the rest of the composition chain passes along None without blowing up or throwing an exception
  Nullable.andThen(safeDivide(4)),
  safeDivide(2),
)(32) // null

compose(
  Nullable.andThen(safeDivide(3)),
  Nullable.andThen(safeDivide(5)),
  Nullable.andThen(safeDivide(4)),
  safeDivide(2),
)(32) // 0.5333333333333333

Nullable.ap

Used for writing in the applicative style. For "lifting" functions into the Nullable context.

Type Annotation
Nullable.ap<A, B>(targetNullable: Nullable<A>): (applicativeNullable: Nullable<(val: A) => B>) => Nullable<B>
Example Usage
// Some arbitrary curried function that takes 3 concrete values:
const addThreeNumbers = (a: number) => (b: number) => (c: number) => a + b + c

compose(
  Nullable.ap(3),
  Nullable.ap(2),
  Nullable.ap(1),
)(addThreeNumbers) // 6

// This can be thought of as "lifting" addThreeNumbers into the context of its passed-in arguments being Nullable:
compose(
  Nullable.ap(3),
  Nullable.ap(null as Nullable<number>), // note we have to typecast this here because TypeScript can’t be sure what kind of Nullable<T> it has at this point.
  Nullable.ap(1),
)(addThreeNumbers) // null