Awesome
ts-nameof
nameof
in TypeScript.
Monorepo for ts-nameof projects:
- ts-nameof (TypeScript compiler)
- babel-plugin-ts-nameof (Babel compiler)
- ts-nameof.macro (Babel compiler)
Recommend: Don't use this package
See here.
Setup
ts-nameof is a compile time transform so it requires some setup. For setup instructions, see the packages above for the compiler you use.
nameof transform
nameof(...)
nameof(console);
nameof(console.log);
nameof(console["warn"]);
Transforms to:
"console";
"log";
"warn";
nameof<T>()
nameof<MyInterface>();
nameof<Array<MyInterface>>();
nameof<MyNamespace.MyInnerInterface>();
Transforms to:
"MyInterface";
"Array";
"MyInnerInterface";
This is useful when working in the type domain.
nameof<T>(o => ...)
nameof<MyInterface>(o => o.prop);
Transforms to:
"prop";
nameof.full transform
nameof.full(...)
nameof.full(console.log);
nameof.full(window.alert.length, 1);
nameof.full(window.alert.length, 2);
nameof.full(window.alert.length, -1);
nameof.full(window.alert.length, -2);
nameof.full(window.alert.length, -3);
Transforms to:
"console.log";
"alert.length";
"length";
"length";
"alert.length";
"window.alert.length";
nameof.full<T>()
nameof.full<MyNamespace.MyInnerInterface>();
nameof.full<MyNamespace.MyInnerInterface>(1);
nameof.full<Array<MyInterface>>();
Transforms to:
"MyNamespace.MyInnerInterface";
"MyInnerInterface";
"Array";
nameof.full<T>(o => ...)
nameof.full<MyInterface>(o => o.prop.prop2);
nameof.full<MyInterface>(o => o.prop.prop2.prop3, 1);
Transforms to:
"prop.prop2";
"prop2.prop3";
nameof.interpolate(value)
Writing the following:
nameof.full(myObj.prop[i]);
...does not interpolate the node in the computed property.
"myObj.prop[i]";
If you want to interpolate the value then you can specify that explicitly with a nameof.interpolate
function.
nameof.full(myObj.prop[nameof.interpolate(i)]);
Transforms to:
`myObj.prop[${i}]`;
nameof.toArray transform
Contributed by: @cecilyth
nameof.toArray(...)
nameof.toArray(myObject, otherObject);
nameof.toArray(obj.firstProp, obj.secondProp, otherObject, nameof.full(obj.other));
Transforms to:
["myObject", "otherObject"];
["firstProp", "secondProp", "otherObject", "obj.other"];
nameof.toArray<T>(o => [...])
nameof.toArray<MyType>(o => [o.firstProp, o.otherProp.secondProp, o.other]);
nameof.toArray<MyType>(o => [o.prop, nameof.full(o.myProp.otherProp, 1)]);
Transforms to:
["firstProp", "secondProp", "other"];
["prop", "myProp.otherProp"];
nameof.split transform
Contributed by: @cecilyth
nameof.split(...)
nameof.split(myObj.prop.prop2);
nameof.split(myObj.prop.prop2, 1);
nameof.split(myObj.prop.prop2, -1);
nameof.split(myObj.prop.prop2).join("/");
Transforms to:
["myObj", "prop", "prop2"];
["prop", "prop2"];
["prop2"];
["myObj", "prop", "prop2"].join("/"); // "myObj/prop/prop2"
nameof.split<T>(o => ...)
nameof.split<MyInterface>(o => o.prop.prop2.prop3);
nameof.split<MyInterface>(o => o.prop.prop2.prop3, 1);
nameof.split<MyInterface>(o => o.prop.prop2.prop3, -1);
nameof.split<IState>(s => s.a.b.c).join("/");
Transforms to:
["prop", "prop2", "prop3"];
["prop2", "prop3"];
["prop3"];
["a", "b", "c"].join("/"); // "a/b/c"