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

typesafe-get

v2.1.2

Published

A typesafe way to get nested properties when any parent property might be undefined, while we wait for the optional chaining operator to finally exist

Downloads

246

Readme

typesafe-get Travis Build Status Uses TypeScript

A typesafe way to access nested properties through potentially-undefined parent properties, while we wait for the optional chaining ?. operator to finally exist and get real TypeScript support.

With typesafe-get, the below TypeScript code will work, without no compile or runtime errors, for all valid values of input:

let input: { a: { b?: { c: null | { d: string } } } } | undefined = ...;
let result: string | undefined = get(input, 'a', 'b', 'c', 'd');

All parameters are validated as properties at the relevant level, and the return type will always be finalPropType | undefined (the type of the property at the very last level, or undefined).

If at any point while following the chain you attempt to follow a path through an undefined object then undefined is immediately returned, and you'll never see the dreaded Cannot read property '<something>' of undefined exception. It works with simple iteration over the property names provided, no crazy (and expensive) try/catch magic here.

Install it

npm install --save typesafe-get

Use it

This module ships with TypeScript types, and is built in JS for UMD, so you should be able to immediately start using it in many environments with no further setup.

With some webpack configurations, you'll need to explicitly allow requiring UMD modules. To do that you can use UMD-Compat-Loader, like so:

module: {
    rules: [
        // ...other rules here
        {
            // You can use (typesafe-get|other-module) if you're using multiple UMD modules
            test: /node_modules[\\|/]typesafe-get/,
            use: { loader: 'umd-compat-loader' }
        }
    ]
}

The get function is exported as both a property and the default export of this module. That means importing looks like:

import { get } from 'typesafe-get';
// or
import get from 'typesafe-get';

To use get:

// Equivalent to obj.aPropertyKey
get(obj, 'aPropertyKey');

// Equivalent to obj.key1.key2, but returning undefined if obj.key1 is undefined:
get(obj, 'key1', 'key2');

// Equivalent to obj.key1.key2.key3.key4.key5, but returning undefined if any step en route is undefined:
get(obj, 'key1', 'key2', 'key3', 'key4', 'key5');

// Equivalent to array[0].key1, but returning undefined if any step en route is undefined:
get(array, 0, 'key1');

Each parameter name is checked against the valid parameter names at that level (so a parameter name that definitely doesn't exist will be caught by TS).

The return type will be automatically inferred as the type the final parameter would have, if the whole chain is defined, or undefined.

get itself supports up to 5 property parameters max. If you really truly honestly need more than that, take a good hard look at yourself, and then feel free to open a PR - it should be fairly easy to see how to extend the types to support more. But only if you're really sure you need this. Seriously, what are you doing that makes this a good idea.

Contributing

Have a bug? File an issue with a simple example that reproduces this so we can take a look & confirm.

Want to make a change? Submit a PR, explain why it's useful, and make sure you've updated the docs (this file) and the tests (see test.ts). You can run the tests with npm test.