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: over 1 year 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 (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-10T11:42:38.000Z (over 1 year ago)
- Last Synced: 2025-03-12T15:41:50.592Z (over 1 year ago)
- Topics: nameof, nameof-operator, typescript
- Language: TypeScript
- Homepage:
- Size: 1010 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
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.test3
nameOf('test1');
// test1
nameOf().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)