Title: Emitter: Static class property "name" causes TypeError due to Function.name being readonly · Issue #9778 · microsoft/TypeScript · GitHub
Open Graph Title: Emitter: Static class property "name" causes TypeError due to Function.name being readonly · Issue #9778 · microsoft/TypeScript
X Title: Emitter: Static class property "name" causes TypeError due to Function.name being readonly · Issue #9778 · microsoft/TypeScript
Description: TypeScript Version: 1.8.10 Consider the following class definition with a static property name: class Foo { static name() { return "Hello World"; } constructor() {} } The TS emitter output for this is: var Foo = (function () { function F...
Open Graph Description: TypeScript Version: 1.8.10 Consider the following class definition with a static property name: class Foo { static name() { return "Hello World"; } constructor() {} } The TS emitter output for this...
X Description: TypeScript Version: 1.8.10 Consider the following class definition with a static property name: class Foo { static name() { return "Hello World"; } constructor() {} } The TS emitter outpu...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/9778
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Emitter: Static class property \"name\" causes TypeError due to Function.name being readonly","articleBody":"\u003c!-- BUGS: Please use this template. -->\n\n**TypeScript Version:** 1.8.10\n\nConsider the following class definition with a static property _name_:\n\n``` ts\nclass Foo {\n static name() {\n return \"Hello World\";\n }\n constructor() {}\n}\n```\n\nThe TS emitter output for this is:\n\n``` javascript\nvar Foo = (function () {\n function Foo() {\n }\n Foo.name = function () {\n return \"Hello World\";\n };\n return Foo;\n}());\n```\n\nHowever, at least in Chrome and Firefox a function's name property is **readonly** for ordinary functions causing the assignment of the static function _name_ to be ignored / causing a TypeError in strict mode.\n\nThe TS example above is also valid ES 2015 so I've tested it without transpilation in a current version of Chromium (v51.0.2704.79). Interestingly, while we can not change the _name_ property of ordinary functions in Chromium we can still do the following for the constructor function once a static _name_ property is present:\n\n``` javascript\nclass Foo {\n static name() {\n return \"Hello World\";\n }\n constructor {}\n}\nFoo.name = \"Hello World\"\nconsole.log(Foo.name); // logs \"Hello World\"\n```\n\nSo the name property of the constructor function is no longer read-only. Based on this I assume Chromium internally does something like\n\n**Expected behavior:**\n\n``` javascript\nvar Foo = (function () {\n function Foo() {\n }\n Object.defineProperty(Foo, \"name\", { writable: true; }); // \u003c= required for static class property *name*\n Foo.name = function () {\n return \"Hello World\";\n };\n return Foo;\n}());\n```\n\nSo a static class property _name_ requires the _name_ property to be made `writable: true` first. Unfortunately, `Object.defineProperty` is only available since ES 5 so static properties could become problematic for a compiler target of `es3`.\n\nTherefore you might also want to note, that section 9.11.2 of ES2015 spec states that the ES interpreter must not set the _name_ property, if a function does have an OwnProperty _name_. So the spec does seem to take custom values for _Function.name_ into account which is why I consider a default configuration of `writable: false` for Function.name being problematic. It would certainly be better if browser vendors could agree here on making Function.name `writable: true` by default.\n","author":{"url":"https://github.com/about-code","@type":"Person","name":"about-code"},"datePublished":"2016-07-17T17:39:30Z","url":"https://github.com/microsoft/TypeScript/issues/9778"}
| 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:beb6b9d6-ebea-a86f-e404-0dc2e566aa99 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D580:3C21B:32D9CF7:40FE9B7:6B01CA50 |
| html-safe-nonce | 32b14582bd813c0b6fa410c89afcfed403f89960614ab3e6e1d35b462cae1444 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENTgwOjNDMjFCOjMyRDlDRjc6NDBGRTlCNzo2QjAxQ0E1MCIsInZpc2l0b3JfaWQiOiI3NTE0ODI3NjA1ODUwMzExMjQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 91005d9ee84be6cdf957388b6a30d883850255ebe4527d9788d6411538c1982b |
| hovercard-subject-tag | issue:165986934 |
| 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/9778/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f1a36b29a94e733b02f5848c8f3f75a4bf554ba00d4363e1e42549ead7c00f4b/microsoft/TypeScript/issues/9778 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f1a36b29a94e733b02f5848c8f3f75a4bf554ba00d4363e1e42549ead7c00f4b/microsoft/TypeScript/issues/9778 |
| og:image:alt | TypeScript Version: 1.8.10 Consider the following class definition with a static property name: class Foo { static name() { return "Hello World"; } constructor() {} } The TS emitter output for this... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | about-code |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width