ts-object-path
Generate strongly-typed deep property path in typescript. Access deep property by a path.
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);
getPath(p.nested.one);
getPath(p.collection[5].nested.two);
getPath(p.three);
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);
get(o, p=> p.nested.one);
get(o, p=> p.collection[1].two);
get(o, p=> p.collection[0].two, 'default');
get(o, p=> p.collection[0].one, 'default');
get(o, p=> p.three);
const val: number = get(o, p=> p.collection[1].two);
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);
set(o, p=> p.nested.one, 3);
set(o, p=> p.collection[1].two, 'hello');