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);
nameOf<NameOfTest>('test1');
nameOf<NameOfTest, never>().test1.test2.test3.$path;
Fabrics
import { nameOf } from 'easy-tsnameof';
type NameOfTest = {
test1: {
test2: {
test3: string;
};
};
};
const f = nameOf<NameOfTest, never>();
f.test1.test2.test3.$path;
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;
Path access methods
.$path
Access to path string:
nameOf<TestType, never>().a.b.c.d.$path;
@m-abboud
.$rawPath
Access to raw path array
Type: (string | number | Symbol)[]
nameOf<TestType, never>().a.b[5].c.d.$rawPath;
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