Title: loading, declaration and execution phases of modules and cyclic references problem ? · Issue #7282 · microsoft/TypeScript · GitHub
Open Graph Title: loading, declaration and execution phases of modules and cyclic references problem ? · Issue #7282 · microsoft/TypeScript
X Title: loading, declaration and execution phases of modules and cyclic references problem ? · Issue #7282 · microsoft/TypeScript
Description: This question is related to TypeScript, ES6, SystemJs and ReflectJs specs, but I would like to know your thoughts about it. So for a function declarations the sequence of code is not important, but for classes it is: Example with single ...
Open Graph Description: This question is related to TypeScript, ES6, SystemJs and ReflectJs specs, but I would like to know your thoughts about it. So for a function declarations the sequence of code is not important, but...
X Description: This question is related to TypeScript, ES6, SystemJs and ReflectJs specs, but I would like to know your thoughts about it. So for a function declarations the sequence of code is not important, but...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/7282
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"loading, declaration and execution phases of modules and cyclic references problem ?","articleBody":"This question is related to TypeScript, ES6, SystemJs and ReflectJs specs, but I would like to know your thoughts about it. \n\nSo for a function declarations the sequence of code is not important, but for classes it is: \n### Example with single module\n\n``` ts\nconsole.info(fnc()); // this is fine\nconsole.info(new Cls()); //TypeError: Cls is not a constructor\nfunction fnc(){\n return \"value\"\n}\nclass Cls {\n constructor(){\n //...\n }\n}\n```\n\nA more complex example when classes located in different modules and have cross references in declaration phase like:\n### Example with multiple modules and static references\n\n``` ts\n// one.ts\nimport {Two} from \"./two\"\nclass One {\n static two = new One();\n}\n// two.ts\nimport {One} from \"./one\"\nclass One {\n static two = new One(); //TypeError: One is not a function \n}\n//main.ts\nimport {One} from \"./one\"\nimport {Two} from \"./two\"\nconsole.info(One.two) \nconsole.info(Two.one)\n\n```\n\nso this is throwing error when trying to create `One` instace, the expectation is to have `One` declared at the time of exection .\n\nThis can be hacked by using some cached getters, which can be somehow used to solve the main problem in runtime(loader) :\n\n``` ts\n// cached.ts\nexport function Cached(target,key,desc){\n var initializer = desc.get;\n desc.get = function(){\n return Object.defineProperty(this,key,{\n value:initializer()\n })[key];\n };\n return desc;\n}\n// one.ts\nimport {Cached} from \"./cached\";\nimport {Two} from \"./two\";\nexport class One {\n @Cached\n static get two():Two{\n return new Two()\n }\n}\n// two.ts\nimport {Cached} from \"./cached\";\nimport {One} from \"./one\";\nexport class Two {\n @Cached\n static get one():One{\n return new One();\n }\n}\n//main.ts\nimport {One} from \"./one\"\nimport {Two} from \"./two\"\nconsole.info(One.two) \nconsole.info(Two.one)\n\n```\n\nI have prepared some demo to show hack on this problem, you can see bellow \n\n**Expected behavior:**\n\n[Create and instance of `Cls` and out put it without errors. ](http://casser.github.io/typescript-cyclic-references-problem/cyclic-hack.html)\n\n**Actual behavior:** \n\n[Throws a `TypeError` because of undeclared class](http://casser.github.io/typescript-cyclic-references-problem/cyclic-problem.html)\n### Example of cyclic references problem in decoration phase\n\nThe other example is [annotators cyclic references problem](http://casser.github.io/typescript-cyclic-references-problem/decors-problem.html).\n\n``` ts\nfunction Model(target){}\nfunction Collection(target){}\nfunction Field(target:any,key:any,desc?:any):any{\n var type:any = Reflect.getMetadata(\"design:type\",target,key);\n document.write(`\u003cpre>${target.constructor.name}.${key} : ${type?type.name:\"\u003cspan style='color: red'>UNDEFINED ?\u003c/span>\"}\u003c/pre>`);\n}\n\n@Model\nexport class User {\n @Field id:String;\n @Field name:String;\n @Field projects:Projects; // Project Is not defined yet\n}\n\n@Collection\nexport class Users extends Array\u003cUser> {}\n\n@Model\nexport class Project {\n @Field id:String;\n @Field name:String;\n @Field author:User;\n @Field contributors:Users; // Users will not be defined \n // if we will change definition order\n}\n\n@Collection\nexport class Projects extends Array\u003cProject>{}\n```\n\nI think this is **IMPORTANT** because `Dependecy Injection (java guice)`, `ORM's` or `Serialization` frameworks will use this feature deeply, and user code can contain _cross referenced types_.\n\nSo the main reason for this problem is that declaration, decoration and execution phases of classes mixed in output. \n\n``` js\n// decoration inside execution block and part of class declaration \n__decorate([\n Field, \n __metadata('design:type', Projects)\n], User.prototype, \"projects\", void 0);\n```\n\nPotencial hack for this can be generate closure instead of direct type referance in metadata like:\n\n``` js\n__decorate([\n Field, \n __metadata('design:type', function(){return Projects})\n], User.prototype, \"projects\", void 0);\n```\n\nwhich can be adjusted after module execution, but I think this is just a hack, and the right way will be finnaly define \nspecification about _Loading > Declaration > Annotation > Decoration and Execution_ phases of runtime which will support cyclic references. \n\nAny thoughts ? \n\nPS: AND AGAIN THANKS FOR `TYPESCRIPT` :)\n","author":{"url":"https://github.com/casser","@type":"Person","name":"casser"},"datePublished":"2016-02-28T15:35:08Z","url":"https://github.com/microsoft/TypeScript/issues/7282"}
| 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:efdb031b-5270-42ca-63bd-5b753faeb1b1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E42C:14F601:27E4BBF:32A0AE3:6B0180FF |
| html-safe-nonce | 069ea03b4f75434c9ec0c10220ece63a82bb5df8db744b34d32ef245d9ac7dfd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNDJDOjE0RjYwMToyN0U0QkJGOjMyQTBBRTM6NkIwMTgwRkYiLCJ2aXNpdG9yX2lkIjoiNTg5NTUyOTY1MTQwODgzMDcxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4c4af29a1f4eb4c0d21e856430c074853c62be220e08511e4ac53ea0405f6f89 |
| hovercard-subject-tag | issue:137064189 |
| 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/7282/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8329dd03e49c389d89ba7eb40c69ef23f9cf1c5366ed7fff9babaa0427670bdc/microsoft/TypeScript/issues/7282 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8329dd03e49c389d89ba7eb40c69ef23f9cf1c5366ed7fff9babaa0427670bdc/microsoft/TypeScript/issues/7282 |
| og:image:alt | This question is related to TypeScript, ES6, SystemJs and ReflectJs specs, but I would like to know your thoughts about it. So for a function declarations the sequence of code is not important, but... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | casser |
| 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