easy-tsnameof
TypeScript icon, indicating that this package has built-in type declarations

3.0.6 • Public • Published

Easy Typescript NameOf

Install

yarn add easy-tsnameof
npm install easy-tsnameof

How to use?

import nameOf from 'easy-tsnameof';

type NameOfTest = {
  test1: {
    test2: {
      test3: string;
    };
  };
};
nameOf<NameOfTest>((o) => o.test1.test2.test3);
// test1.test2.test3

nameOf<NameOfTest>('test1');
// test1

nameOf<NameOfTest, never>().test1.test2.test3.$path;
// test1.test2.test3

Fabrics

import { nameOf } from 'easy-tsnameof';

type NameOfTest = {
  test1: {
    test2: {
      test3: string;
    };
  };
};
const f = nameOf<NameOfTest, never>();
f.test1.test2.test3.$path;
// test1.test2.test3

Working with arrays

import { nameOf } from 'easy-tsnameof';

type NameOfTest = {
  test1: {
    test2: {
      test: string;
    };
    test3: { test4: number }[];
  };
};
const f = nameOf<NameOfTest, never>();
const index = 999;

f.test1.test3[index].test4.$path;
// test1.test3[999].test4

Path access methods

.$path

Access to path string:

nameOf<TestType, never>().a.b.c.d.$path;
// "a.b.c.d"

@m-abboud

.$rawPath

Access to raw path array

Type: (string | number | Symbol)[]

nameOf<TestType, never>().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:

Inspired by

Readme

Keywords

Package Sidebar

Install

npm i easy-tsnameof

Weekly Downloads

4

Version

3.0.6

License

MIT

Unpacked Size

55.2 kB

Total Files

18

Last publish

Collaborators

  • kolengri