question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Add the nameof compile-time operator to convert property and function names into strings

See original GitHub issue

I would like to see the nameof operator be considered for Typescript.

This feature was just added to C# description, and it is an elegant solution to a common issue in Javascript.

At compile time, nameof converts its parameter (if valid), into a string. It makes it much easier to reason about “magic strings” that need to match variable or property names across refactors, prevent spelling mistakes, and other type-safe features.

To quote from the linked C# article:

Using ordinary string literals for this purpose is simple, but error prone. You may spell it wrong, or a refactoring may leave it stale. nameof expressions are essentially a fancy kind of string literal where the compiler checks that you have something of the given name, and Visual Studio knows what it refers to, so navigation and refactoring will work:

(if x == null) throw new ArgumentNullException(nameof(x));

To show another example, imagine you have this Person class:

class Person {
    firstName: string
    lastName: string
}
var instance : Person = new Person();

If I have an API that requires me to specify a property name by string (pretty common in JS), I am forced to do something like this:

   someFunction(personInstance, "firstName");

But if I misspell firstName, I’ll get a runtime error. So this is the type-safe equivalent:

   someFunction(personInstance, nameof(Person.firstName));

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:329
  • Comments:174 (42 by maintainers)

github_iconTop GitHub Comments

96reactions
grokky1commented, Sep 24, 2017

Why after all this time, TS still has no mechanism to (e.g. nameof) to eliminate MAGIC STRINGS?

I don’t understand why this thread has so much begging, justifications, and use cases. It’s a no brainer - this should be a top priority. It’s the most horrible part of JS which TS has not yet solved.

20reactions
zpdDG4gta8XKpMCdcommented, Jul 29, 2016

i don’t quite get this reluctance either

nameof is merely a macros that transforms a property name identifier into a string literal at the syntax level, why is it such a big deal to add it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

A standard way for getting variable name at compile time
We do have to write the variable name, but if we implement the stringification macro in a certain way, compiler will be able...
Read more >
TypeScript - nameof operator equivalent - Meziantou's blog
Suggestion : Add the nameof compile-time operator to convert property and function names into strings. Do you have a question or a suggestion...
Read more >
The nameof expression - evaluate the text name of a symbol
A nameof expression is evaluated at compile time and has no effect at run time. When the operand is a type or a...
Read more >
typescript get property by string name - Lena's Italian Restaurant
Suggestion : Add the nameof compile-time operator to convert property To use a class as a type, you use the class name in...
Read more >
Built-in Functions — Python 3.11.1 documentation
This function can also be used to execute arbitrary code objects (such as those created by compile() ). In this case, pass a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found