Nested Object Access
![NPM Downloads](https://img.shields.io/npm/dm/nested-object-access.svg?style=flat-square)
TypeScript-powered util to work with nested objects using dot-separated keys.
Get it on NPM here
This package exports the following stuff:
Helper type: NestedKeys
Returns a union of all dot-separated paths to entries in a nested object.
type TestDict = {
b: string;
c: { d: number; w: string; e: { p: false } };
t: { l: boolean; e: { q: "foo" } };
i: { d: string | undefined; o: null };
};
type TestKeys = NestedKeys<TestDict>;
type TestKeysPr = NestedKeys<TestDict, "primitives">;
type TestKeysObj = NestedKeys<TestDict, "objects">;
type TestKeysWithDepth = NestedKeys<TestDict, any, 2>;
Helper type: RetrieveNested
Returns the type that is at a dot-separated path in an object.
type TestDict2 = { c: { e: { p: "pp" } } }
type TestNestedPath1 = RetrieveNested<TestDict2, "c">;
type TestNestedPath2 = RetrieveNested<TestDict2, "c.e">;
type TestNestedPath3 = RetrieveNested<TestDict2, "c.e.p">;
Function: getNested
Function version of RetrieveNested.
import { getNested } from "nested-object-access";
const testDict = { c: { e: { p: "pp" } } };
console.log(getNested(testDict, "c.e.p"));
TODO
- You can currently only pass a
type Foo = {...}
into the helper functions, not interface Foo {...}
because of missing index signature...