• Stars
    star
    486
  • Rank 90,527 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

nameof in TypeScript

ts-nameof

Build Status

nameof in TypeScript.

Monorepo for ts-nameof projects:

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"

Other

More Repositories

1

ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
TypeScript
4,452
star
2

ts-ast-viewer

TypeScript AST viewer.
TypeScript
1,126
star
3

dax

Cross-platform shell tools for Deno and Node.js inspired by zx.
TypeScript
984
star
4

conditional-type-checks

Types for testing TypeScript types.
TypeScript
448
star
5

code-block-writer

Code writer for JavaScript and TypeScript code.
TypeScript
152
star
6

ts-type-info

TypeScript AST and code generator [Deprecated]
TypeScript
93
star
7

ts-factory-code-generator-generator

Generates code that generates TypeScript Compiler API factory code from an AST.
TypeScript
86
star
8

using_statement

"Using statement" in JavaScript and TypeScript.
TypeScript
68
star
9

rust-toolchain-file

GitHub Action to install Rust's toolchain via rust-toolchain.toml
28
star
10

dts-minify

Minifier for TypeScript declaration files (.d.ts)
TypeScript
24
star
11

barrel-maintainer

Automated real-time maintenance of barrels in JavaScript and TypeScript.
TypeScript
19
star
12

deno-which

Finds the path to the specified command in Deno.
TypeScript
14
star
13

console_static_text

Logging for text that should stay in the same place in a console.
Rust
13
star
14

jsr-publish-on-tag

Publishes a package to JSR with a version based on the current git tag.
TypeScript
13
star
15

Units-of-Measure-Proposal-for-TypeScript

11
star
16

server-bridge

Code generation for a statically typed bridge between the client and server in TypeScript
TypeScript
10
star
17

npm_bridge

Proof of concept for a bridge between Deno and npm packages
TypeScript
7
star
18

faster_prettier_example

TypeScript
6
star
19

which-runtime

Deno module for checking which runtime the code is running in.
TypeScript
6
star
20

CodeBlockWriterSharp

Code writer that assists with formatting and visualizing blocks of JavaScript or TypeScript code in c#.
C#
6
star
21

npm-to-deno-workspace-example

TypeScript
4
star
22

text_lines

Information about lines of text in a string.
Rust
2
star
23

ts-object-create

Code generation that writes functions for creating objects with their types.
TypeScript
2
star
24

npm_bridge_example

Example of using npm bridge
TypeScript
2
star
25

libpack

Module concatenator for large Deno libraries.
Rust
2
star
26

slow-wasm-example

Rust
1
star
27

tsconf-talk

Code and slides for my talk at tsconf 2018.
TypeScript
1
star
28

ts-state-test-generator

Generates test helper functions for checking the state of a class or interface [abandoned]
TypeScript
1
star
29

path

Path class for JavaScript built on top of Deno's standard library.
TypeScript
1
star