{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16395","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16395/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16395/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16395/events","html_url":"https://github.com/Microsoft/TypeScript/issues/16395","id":234817148,"number":16395,"title":"Class interdependency and decorators","user":{"login":"lifaon74","id":6762756,"avatar_url":"https://avatars0.githubusercontent.com/u/6762756?v=3","gravatar_id":"","url":"https://api.github.com/users/lifaon74","html_url":"https://github.com/lifaon74","followers_url":"https://api.github.com/users/lifaon74/followers","following_url":"https://api.github.com/users/lifaon74/following{/other_user}","gists_url":"https://api.github.com/users/lifaon74/gists{/gist_id}","starred_url":"https://api.github.com/users/lifaon74/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lifaon74/subscriptions","organizations_url":"https://api.github.com/users/lifaon74/orgs","repos_url":"https://api.github.com/users/lifaon74/repos","events_url":"https://api.github.com/users/lifaon74/events{/privacy}","received_events_url":"https://api.github.com/users/lifaon74/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-06-09T12:51:12Z","updated_at":"2017-06-13T23:02:24Z","closed_at":null,"body":"**TypeScript Version:**  2.3.2\r\n**Build options:**  target: \"esnext\"\r\n\r\n**Code**\r\nIn this example, the class A has an attribute decorated with a type B, and the class B has an attribute decorated with a type A.\r\n\r\n```ts\r\nfunction Decorator() {}\r\n\r\nclass A {\r\n  @Decorator b: B;\r\n}\r\n\r\nclass B {\r\n  @Decorator a: A;\r\n}\r\n```\r\n\r\nIs compiled to (with esnext as target) : \r\n\r\n```js\r\nfunction Decorator() {\r\n}\r\nclass A {\r\n}\r\n__decorate([\r\n    Decorator,\r\n    __metadata(\"design:type\", B)\r\n], A.prototype, \"b\", void 0);\r\nclass B {\r\n}\r\n__decorate([\r\n    Decorator,\r\n    __metadata(\"design:type\", A)\r\n], B.prototype, \"a\", void 0);\r\n```\r\n\r\n**Expected behavior:**\r\nThe code should be runnable with es6 like es5.\r\n\r\n**Actual behavior:**\r\nRunning the code triggers an error : \r\n```\r\n__metadata(\"design:type\", B)\r\n                              ^\r\nReferenceError: B is not defined\r\n```\r\n\r\nBecause class declaration are not [hoisted](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting), the class B doesn't already exists when the decorator of the class A is called.\r\n\r\n**Possible solution:**\r\n1) Create a [global](https://github.com/tc39/proposal-global) variable : \r\n```js\r\nvar global = (function() {\r\n  if(typeof global !== 'undefined') return global;\r\n  if(typeof self !== 'undefined') return self;\r\n  if(typeof window !== 'undefined') return window;\r\n  return {};\r\n})();\r\n```\r\n2) Test if class exists or provide Object as default: \r\n```js\r\n __metadata(\"design:type\", ('B' in global) ? B : Object)\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/308272916","html_url":"https://github.com/Microsoft/TypeScript/issues/16395#issuecomment-308272916","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/16395","id":308272916,"user":{"login":"mhegazy","id":8000722,"avatar_url":"https://avatars0.githubusercontent.com/u/8000722?v=3","gravatar_id":"","url":"https://api.github.com/users/mhegazy","html_url":"https://github.com/mhegazy","followers_url":"https://api.github.com/users/mhegazy/followers","following_url":"https://api.github.com/users/mhegazy/following{/other_user}","gists_url":"https://api.github.com/users/mhegazy/gists{/gist_id}","starred_url":"https://api.github.com/users/mhegazy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mhegazy/subscriptions","organizations_url":"https://api.github.com/users/mhegazy/orgs","repos_url":"https://api.github.com/users/mhegazy/repos","events_url":"https://api.github.com/users/mhegazy/events{/privacy}","received_events_url":"https://api.github.com/users/mhegazy/received_events","type":"User","site_admin":false},"created_at":"2017-06-13T23:02:24Z","updated_at":"2017-06-13T23:02:24Z","body":"Duplicate of https://github.com/Microsoft/TypeScript/issues/4114 and https://github.com/Microsoft/TypeScript/issues/4521"}}
{"actor":{"display_login":"mhegazy"}}