Title: Suggestion: Add the nameof compile-time operator to convert property and function names into strings · Issue #1579 · microsoft/TypeScript · GitHub
Open Graph Title: Suggestion: Add the nameof compile-time operator to convert property and function names into strings · Issue #1579 · microsoft/TypeScript
X Title: Suggestion: Add the nameof compile-time operator to convert property and function names into strings · Issue #1579 · microsoft/TypeScript
Description: 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)...
Open Graph Description: 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, ...
X Description: 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, ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/1579
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Suggestion: Add the nameof compile-time operator to convert property and function names into strings","articleBody":"I would like to see the `nameof` operator be considered for Typescript.\n\nThis feature was just added to C# [description](http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx), and it is an elegant solution to a common issue in Javascript.\n\nAt 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.\n\nTo quote from the linked C# article:\n\n> 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:\n\n`(if x == null) throw new ArgumentNullException(nameof(x));`\n\nTo show another example, imagine you have this Person class:\n\n```\nclass Person {\n firstName: string\n lastName: string\n}\nvar instance : Person = new Person();\n```\n\nIf 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:\n\n```\n someFunction(personInstance, \"firstName\");\n```\n\nBut if I misspell `firstName`, I'll get a runtime error.\nSo this is the type-safe equivalent: \n\n```\n someFunction(personInstance, nameof(Person.firstName));\n```\n","author":{"url":"https://github.com/Taytay","@type":"Person","name":"Taytay"},"datePublished":"2014-12-30T22:17:37Z","url":"https://github.com/microsoft/TypeScript/issues/1579"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9746d5b8-16fc-968b-a791-5d8533e4ceba |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF2A:2454CC:67CBE5:85E31C:6B00E128 |
| html-safe-nonce | be7dded50835500e5716db136877abe4c0263f9893dac58dfe6a8bf4a5aeb487 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjJBOjI0NTRDQzo2N0NCRTU6ODVFMzFDOjZCMDBFMTI4IiwidmlzaXRvcl9pZCI6IjI0MjY0ODM1MjcwNDc1MDQxNjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f0b8113c2d5ae37c9f4dfc66a385ca882c0c93387325befb1c713b65fe2dfe7a |
| hovercard-subject-tag | issue:53151928 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/1579/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c07fa65fe5809f871e216ab8f002294d0c6fa849636788310ee902072496edf2/microsoft/TypeScript/issues/1579 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c07fa65fe5809f871e216ab8f002294d0c6fa849636788310ee902072496edf2/microsoft/TypeScript/issues/1579 |
| og:image:alt | 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, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Taytay |
| hostname | github.com |
| expected-hostname | github.com |
| None | 043c3f30fbcbe2c6c8e729e10cbbbc90c406546f9fbbb67c648d38f6b7d16daf |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5425dc24e8bd8d6fca9641a12afc984825a43875 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width