Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ts-object-path

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-object-path

Generate strongly-typed deep property path in typescript. Access deep property by a path.

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8.3K
decreased by-10.69%
Maintainers
1
Weekly downloads
 
Created
Source

ts-object-path

Generate strongly-typed deep property path in typescript. Access deep property by a path.

npm version

Install

npm install ts-object-path --save

Usage

Get property path

import { createProxy, getPath } from 'ts-object-path'

interface IExample {
  one: number;
  two: string;
  nested: IExample;
  collection: IExample[];
}

const p = createProxy<IExample>();

getPath(p.one); // returns ['one']
getPath(p.nested.one); // returns ['nested', 'one']
getPath(p.collection[5].nested.two); // returns ['collection', 5, 'nested', 'two']
getPath(p.three); // compilation error (no such property)

Get deep property value

import { get } from 'ts-object-path'

interface IExample {
  one?: number;
  two?: string;
  nested?: IExample;
  collection?: IExample[];
}

const o: IExample = {
  one: 777;
  collection: [
    null,
    { two: 'Hello' }
  ]
};

get(o, p=> p.one); // returns 777
get(o, p=> p.nested.one); // returns undefined
get(o, p=> p.collection[1].two); // returns 'Hello'
get(o, p=> p.collection[0].two, 'default'); // returns 'default'
get(o, p=> p.collection[0].one, 'default'); // compilation error (property and default value types don't match)
get(o, p=> p.three); // compilation error (no such property)
const val: number = get(o, p=> p.collection[1].two); // compilation error (string is not assignable to number)

Set deep property value

import { set } from 'ts-object-path'

interface IExample {
  one?: number;
  two?: string;
  nested?: IExample;
  collection?: IExample[];
}

const o: IExample = {
  one: 1;
};

set(o, p=> p.one, 777); // o === { one: 777 }
set(o, p=> p.nested.one, 3); // o === { one: 777, nested: { one: 3 } }
set(o, p=> p.collection[1].two, 'hello'); // o === { one: 777, nested: { one: 3 }, collection: [undefined, { two: 'hello'}] }

FAQs

Package last updated on 19 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc