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

ts-not-nil

v1.0.1

Published

A runtime assertion for preserving not-null and not-undefined invariants

Downloads

18

Readme

npm version Build Status TypeScript

A runtime assertion for preserving not-null and not-undefined invariants.

:zap: Rationale

This package helps you to prevent nasty "Cannot read property 'a' of undefined/null" runtime error messages with some compile-time sanity checks (thanks to TypeScript).

This library exports two functions:

/** 
 * @throws NotNilAssertionError
 */
export function assertNotNil<T>(val: T, reason?: string): asserts val is NonNullable<T>;

/**
 * @throws NotNilAssertionError
 */
export function unwrapNotNil<T>(val: T, reason?: string): NonNullable<T>;

If val happens to be null or undefined, these functions throw NotNilAssertionError at runtime. There is also a debugger statement that is hit when the assertion fails for debugging convenience.

If val is neither null nor undefined nothing happens.

The difference between assertNotNil(val) and unwrapNotNil(val) is that the former returns val back which is handy for inline assertions inside of expressions.

The second optional parameter reason will be embedded into the thrown error message in case of an assertion failure.

const map = new Map<string, number>();
map.set("key", 42);

const foo: number | undefined = map.get("key");

assertNotNil(foo, `I've added an entry with key "key" one line above!`);

foo; // `number` - its type was narrowed to just number after assertion

const bar: number = unwrapNotNil(map.get("key"), "Believe me!"); // inline assertion

// All the following invocations throw
assertNotNil(null);
assertNotNil(undefined);
unwrapNotNil(null);
unwrapNotNil(undefined);

:boom: Compile-time protection

Both of the exported functions help you to catch bugs at compile time. They explicitly prohibit calling them with T that is known to be neither null nor undefined at compile time. This is quite a hack with the type system, that may generate an error report that says you didn't pass the second parameter to *notNil() function or that the parameter is not assignable to NotNilAssertionCompileError, but as long as you see which line it points to and see assertNotNil(val) or unwrapNotNil(val) invocation you should rethink whether this assertion is really needed (because the type system already ensures that val is neither null nor undefined).

declare const unkown:     unknown;
declare const any:        unknown;
declare const maybeNull:  string | null;
declare const maybeUndef: string | undefined;
declare const maybeNil:   string | null | undefined;
declare const string:     string;
declare const obj:        { a: number };

assertNotNil(unkown);     // ok
assertNotNil(any);        // ok
assertNotNil(maybeNull);  // ok
assertNotNil(maybeUndef); // ok
assertNotNil(maybeNil);   // ok
assertNotNil(string);     // compile error: the value passed is known to never be nil
assertNotNil(obj);        // compile error: the value passed is known to never be nil

:warning: Caveats

You should have "strictNullChecks" enabled in your tsconfig.json and that's all.