Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

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)