https://github.com/johnrom/ts-object-paths
https://github.com/johnrom/ts-object-paths
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/johnrom/ts-object-paths
- Owner: johnrom
- Created: 2021-05-04T22:33:18.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-04T22:33:52.000Z (about 5 years ago)
- Last Synced: 2025-08-31T10:51:47.843Z (10 months ago)
- Language: TypeScript
- Size: 44.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `ts-object-paths` EXPERIMENTAL!
Given a Nested Object, relate paths with values. Uses template literal types -- it will fall back to `string` | `any` situationally on `TS<4.1`.
Uhh, not to be confused with `ts-object-path`. I should do better.
### Check out the API Reference
- [Api Reference](./docs/API.md)
### Code examples
```ts
interface BasePerson = {
name: {
first: string;
last: string;
};
motto: string;
nicknames: string[];
age: number;
favoriteNumbers: number[];
rootStrPath: 'rootStrValue';
arrayStrPath: 'arrayStrValue'[];
};
interface Person extends BasePerson {
partner: BasePerson;
friends: BasePerson[];
};
//
// Get all possible paths of an object
//
const anyPath: PathOf[] = [
'name.first',
'name.last',
'motto',
'nicknames.0',
// etc
]
//
// Get paths that are assignable to a given value type, e.g., `string`:
//
const strMatches: PathMatchingValue[] = [
'name.first',
'name.last',
'motto',
'nicknames.0', // etc
'partner.name.first', // etc
'friends.0.nicknames.0', // etc
];
// @ts-expect-error these paths don't match `string`!
const badMatches: PathMatchingValue[] = [
// nope, it's a number
'age',
// nope, not a "string", 'rootStrValue' is a constant
'rootStrPath'
];
//
// Get the value of a given path:
//
const value: ValueMatchingPath = "it's a string";
// @ts-expect-error
const value: ValueMatchingPath = "NOT A STRING! ERROR!";
```
## Authors
- [John Rom](https://johnrom.com)
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
## Acknowledgments
- TypeScript team for landing something as insane as template literal types.
- TypeScript discord community for putting up with a million questions.