Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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)