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
; getPathp.one; // returns ['one']getPathp.nested.one; // returns ['nested', 'one']getPathp.collection.nested.two; // returns ['collection', 5, 'nested', 'two']getPathp.three; // compilation error (no such property)
Get deep property value
; geto, p.one; // returns 777geto, p.nested.one; // returns undefinedgeto, p.collection.two; // returns 'Hello'geto, p.collection.two, 'default'; // returns 'default'geto, p.collection.one, 'default'; // compilation error (property and default value types don't match)geto, p.three; // compilation error (no such property); // compilation error (string is not assignable to number)
Set deep property value
; seto, p.one, 777; // o === { one: 777 }seto, p.nested.one, 3; // o === { one: 777, nested: { one: 3 } }seto, p.collection.two, 'hello'; // o === { one: 777, nested: { one: 3 }, collection: [undefined, { two: 'hello'}] }