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
Last updated 2 years ago by ikerin .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install @ovotech/typesafe-get 
SYNC missed versions from official npm registry.

Typesafe get

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

With typesafe get, the below TypeScript code will run, without throwing any exceptions, 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.


yarn add @ovotech/typesafe-get

This module ships with TypeScript types, so you should be able to immediately start using it in any environment with no further setup.

import { get } from 'typesafe-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');

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 does not support array indexes, only string-indexed object properties. This is a typing limitation (the runtime implementation can actually handle this).

Running the tests

yarn test

Coding style (linting, etc) tests

Style is maintained with prettier and tslint

yarn lint


To deploy a new version, push to master and then create a new release. CircleCI will automatically build and deploy a the version to the npm registry.


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/index.spec.ts). You can run the tests with yarn test.

Responsible Team

  • Boost Internal Tools (BIT)


This project is licensed under the MIT - see the LICENSE file for details


Current Tags

  • 2.0.1                                ...           latest (2 years ago)

2 Versions

  • 2.0.1                                ...           2 years ago
  • 2.0.0                                ...           2 years ago
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
Dev Dependencies (8)
Dependents (0)

Copyright 2014 - 2016 © taobao.org |