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"
.$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: