FG
💻 Software🌐 Web & Full-StackMicrosoft

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

Freshabout 2 years ago
Mar 14, 20260 views
Confidence Score79%
79%

Problem

I would like to see the `nameof` operator be considered for Typescript. This feature was just added to Cdescription, 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 Carticle: > 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: [code block] 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: [code block] But if I misspell `firstName`, I'll get a runtime error. So this is the type-safe equivalent: [code block]

Error Output

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 nam

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
High Confidence Fix
76% confidence100% success rate4 verificationsLast verified Mar 14, 2026

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

Low Risk

Agree. Doesnt look like a duplicate. A nameof operator should be considered as a very good idea. Probably "easy" to implement too. I use typescript with angularjs and I like to create static string constants inside my controller/directive classes that I use when I register these classes in an angular module. Instead of hardcoding these names I would like to use nameof(MyDirectiveClass) instead. It

76

Trust Score

4 verifications

100% success
  1. 1

    Agree. Doesnt look like a duplicate. A nameof operator should be considered as a

    I use typescript with angularjs and I like to create static string constants inside my controller/directive classes that I use when I register these classes in an angular module. Instead of hardcoding these names I would like to use nameof(MyDirectiveClass) instead. Its the same kind of problem that we experienced earlier with PropertyChanged events in WPF (before nameof/CallerMemberName) when you renamed stuff.

Validation

Resolved in microsoft/TypeScript GitHub issue #1579. Community reactions: 7 upvotes.

Verification Summary

Worked: 4
Partial: 1
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typescriptcompilersuggestionwaiting-for-tc39