Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kolengri/easy-tsnameof
🍫 Make nameof in typescript sweet again! Type safe your interface path
https://github.com/kolengri/easy-tsnameof
nameof nameof-operator typescript
Last synced: 16 days ago
JSON representation
🍫 Make nameof in typescript sweet again! Type safe your interface path
- Host: GitHub
- URL: https://github.com/kolengri/easy-tsnameof
- Owner: kolengri
- License: mit
- Created: 2020-05-14T05:40:13.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T18:10:08.000Z (almost 2 years ago)
- Last Synced: 2024-12-15T03:24:21.296Z (19 days ago)
- Topics: nameof, nameof-operator, typescript
- Language: TypeScript
- Homepage:
- Size: 1.12 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Easy Typescript NameOf
## Install
```bash
yarn add easy-tsnameof
``````bash
npm install easy-tsnameof
```## How to use?
```ts
import nameOf from 'easy-tsnameof';type NameOfTest = {
test1: {
test2: {
test3: string;
};
};
};
nameOf((o) => o.test1.test2.test3);
// test1.test2.test3nameOf('test1');
// test1nameOf().test1.test2.test3.$path;
// test1.test2.test3
```## Fabrics
```ts
import { nameOf } from 'easy-tsnameof';type NameOfTest = {
test1: {
test2: {
test3: string;
};
};
};
const f = nameOf();
f.test1.test2.test3.$path;
// test1.test2.test3
```## Working with arrays
```ts
import { nameOf } from 'easy-tsnameof';type NameOfTest = {
test1: {
test2: {
test: string;
};
test3: { test4: number }[];
};
};
const f = nameOf();
const index = 999;f.test1.test3[index].test4.$path;
// test1.test3[999].test4
```## Path access methods
### .$path
Access to path string:
```ts
nameOf().a.b.c.d.$path;
// "a.b.c.d"
```[@m-abboud](https://github.com/m-abboud)
### .$rawPath
Access to raw path array
Type: `(string | number | Symbol)[]`
```ts
nameOf().a.b[5].c.d.$rawPath;
// ["a", "b", 5, "c", "d"]
```The `$rawPath` is something that you might want to use with the following methods from
Ramda, to add type safety on the path:- [R.assocPath](https://ramdajs.com/docs/#assocPath),
- [R.dissocPath](https://ramdajs.com/docs/#dissocPath),
- [R.hasPath](https://ramdajs.com/docs/#hasPath),
- [R.path](https://ramdajs.com/docs/#path),
- [R.pathEq](https://ramdajs.com/docs/#pathEq),
- [R.pathOr](https://ramdajs.com/docs/#pathOr),
- [R.paths](https://ramdajs.com/docs/#paths),
- [R.lensPath](https://ramdajs.com/docs/#lensPath)## Inspired by
- [typed-path](https://github.com/bsalex/typed-path)