What do you think about a C# 6 like nameof() expression for

Ron Buckton Ron.Buckton at microsoft.com
Fri Jun 14 16:29:05 UTC 2019

The 'nameof' operator provides the string name of a static symbol (in compiler/linker terms, not an ECMAScript 'Symbol'). This is often useful for diagnostics (logging and errors), and gives developers a way to reduce repetition. It is also extremely helpful in editors that support symbolic "rename" refactoring.

While ES functions have a 'name' property, one of the main use cases is to get a string representation of the name of a thing that isn't itself reified as an object, such as a variable, parameter, or module namespace:

import * as ns from "foo";

nameof ns; // "ns"

let fn = function g() {};
fn.name; // "g"
nameof fn; // "fn"

Here is an example of two common use cases:

class C {
  compare(other) {
    if (!(other instanceof C)) {
      throw new TypeError(`Invalid argument: ${nameof other} `);

  set prop(value) {
    this._prop = value;
    this.emit("propertychanged", nameof prop);

It's not that a language like TypeScript would need this, but rather that users of an editor like VS Code or WebStorm that have a JavaScript language service would benefit from it.

From: es-discuss <es-discuss-bounces at mozilla.org> on behalf of guest271314 <guest271314 at gmail.com>
Sent: Friday, June 14, 2019 9:05:55 AM
To: Stas Berkov
Cc: es-discuss at mozilla.org
Subject: Re: Re: What do you think about a C# 6 like nameof() expression for

Have not tried TypeScript. What are the use cases for JavaScript where TypeScript is not used? Does ```nameof()``` check if an object has a specific property name defined (```"MutationObserver" in window```; ```window.MutationObserver```; ```"document" in globalThis```)?

On Fri, Jun 14, 2019 at 1:05 PM Stas Berkov <stas.berkov at gmail.com<mailto:stas.berkov at gmail.com>> wrote:
Can we revisit this issue?

In C# there is `nameof`, in Swift you can do the same by calling
let keyPath = \Person.mother.firstName
NSPredicate(format: "%K == %@", keyPath, "Andrew")
Let's introduce `nameof` in ES, please.

Devs from TypeScript don't want to introduce this feature in TypeScript unless it is available in ES ( https://github.com/microsoft/TypeScript/issues/1579<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmicrosoft%2FTypeScript%2Fissues%2F1579&data=02%7C01%7Cron.buckton%40microsoft.com%7Ceab205c181fc4322b92208d6f0e23843%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C636961251760815455&sdata=qeHDrSKENDcCczwAJFoOqAro5lo9NcXzBJA5eCXx9rA%3D&reserved=0> )
This feature is eagarly being asked by TypeScript community.

I understand there are couple issues related to `nameof` feature in ES. They are: minification and what to do if user already has `nameof` function.

1. If your code to be minimized be prepared that variable names will also change.
2. (just a possibility) Minimizer can have option to replace `nameof(someVar)` with result of `nameof` function.

What if user already has `nameof` function.
1. To maintain status quo we can user `nameof` function having priority over newly introduced language feature.
2. OR we can use `typeof` syntax, e.g. `nameof msg.userName` (// returns "userName" string)
es-discuss mailing list
es-discuss at mozilla.org<mailto:es-discuss at mozilla.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190614/b86bd0aa/attachment-0001.html>

More information about the es-discuss mailing list