Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dsherret/ts-nameof
nameof in TypeScript
https://github.com/dsherret/ts-nameof
babel-plugin custom-transformer typescript
Last synced: 5 days ago
JSON representation
nameof in TypeScript
- Host: GitHub
- URL: https://github.com/dsherret/ts-nameof
- Owner: dsherret
- License: mit
- Created: 2016-08-07T19:57:40.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2023-03-23T16:04:22.000Z (over 1 year ago)
- Last Synced: 2024-11-30T21:07:35.177Z (13 days ago)
- Topics: babel-plugin, custom-transformer, typescript
- Language: TypeScript
- Homepage:
- Size: 922 KB
- Stars: 493
- Watchers: 6
- Forks: 23
- Open Issues: 33
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-ccamel - dsherret/ts-nameof - nameof in TypeScript (TypeScript)
- awesome-luooooob - dsherret/ts-nameof - nameof in TypeScript (TypeScript)
- awesome-resources - nameOF - TS
- awesome-typescript-ecosystem - ts-nameof - nameof in TypeScript (Transformers / General transformers)
README
# ts-nameof
[![Build Status](https://travis-ci.org/dsherret/ts-nameof.svg)](https://travis-ci.org/dsherret/ts-nameof)
[`nameof`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof) in TypeScript.
Monorepo for ts-nameof projects:
- [ts-nameof](packages/ts-nameof) (TypeScript compiler)
- [babel-plugin-ts-nameof](packages/babel-plugin-ts-nameof) (Babel compiler)
- [ts-nameof.macro](packages/ts-nameof.macro) (Babel compiler)## Recommend: Don't use this package
See [here](https://github.com/dsherret/ts-nameof/issues/121).
## 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(...)`
```ts
nameof(console);
nameof(console.log);
nameof(console["warn"]);
```Transforms to:
```ts
"console";
"log";
"warn";
```### `nameof()`
```ts
nameof();
nameof>();
nameof();
```Transforms to:
```ts
"MyInterface";
"Array";
"MyInnerInterface";
```This is useful when working in the type domain.
### `nameof(o => ...)`
```ts
nameof(o => o.prop);
```Transforms to:
```ts
"prop";
```## nameof.full transform
### `nameof.full(...)`
```ts
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:
```ts
"console.log";
"alert.length";
"length";
"length";
"alert.length";
"window.alert.length";
```### `nameof.full()`
```ts
nameof.full();
nameof.full(1);
nameof.full>();
```Transforms to:
```ts
"MyNamespace.MyInnerInterface";
"MyInnerInterface";
"Array";
```### `nameof.full(o => ...)`
```ts
nameof.full(o => o.prop.prop2);
nameof.full(o => o.prop.prop2.prop3, 1);
```Transforms to:
```ts
"prop.prop2";
"prop2.prop3";
```### `nameof.interpolate(value)`
Writing the following:
```ts
nameof.full(myObj.prop[i]);
```...does not interpolate the node in the computed property.
```ts
"myObj.prop[i]";
```If you want to interpolate the value then you can specify that explicitly with a `nameof.interpolate` function.
```ts
nameof.full(myObj.prop[nameof.interpolate(i)]);
```Transforms to:
```ts
`myObj.prop[${i}]`;
```## nameof.toArray transform
Contributed by: [@cecilyth](https://github.com/cecilyth)
### `nameof.toArray(...)`
```ts
nameof.toArray(myObject, otherObject);
nameof.toArray(obj.firstProp, obj.secondProp, otherObject, nameof.full(obj.other));
```Transforms to:
```ts
["myObject", "otherObject"];
["firstProp", "secondProp", "otherObject", "obj.other"];
```### `nameof.toArray(o => [...])`
```ts
nameof.toArray(o => [o.firstProp, o.otherProp.secondProp, o.other]);
nameof.toArray(o => [o.prop, nameof.full(o.myProp.otherProp, 1)]);
```Transforms to:
```ts
["firstProp", "secondProp", "other"];
["prop", "myProp.otherProp"];
```## nameof.split transform
Contributed by: [@cecilyth](https://github.com/cecilyth)
### `nameof.split(...)`
```ts
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:
```ts
["myObj", "prop", "prop2"];
["prop", "prop2"];
["prop2"];
["myObj", "prop", "prop2"].join("/"); // "myObj/prop/prop2"
```### `nameof.split(o => ...)`
```ts
nameof.split(o => o.prop.prop2.prop3);
nameof.split(o => o.prop.prop2.prop3, 1);
nameof.split(o => o.prop.prop2.prop3, -1);
nameof.split(s => s.a.b.c).join("/");
```Transforms to:
```ts
["prop", "prop2", "prop3"];
["prop2", "prop3"];
["prop3"];
["a", "b", "c"].join("/"); // "a/b/c"
```## Other
- [Contributing](CONTRIBUTING.md)
- [Development](DEVELOPMENT.md)