Title: [Proposal] External Module Type · Issue #13231 · microsoft/TypeScript · GitHub
Open Graph Title: [Proposal] External Module Type · Issue #13231 · microsoft/TypeScript
X Title: [Proposal] External Module Type · Issue #13231 · microsoft/TypeScript
Description: Introduction Currently, TypeScript allows to ensure typings on differents levels using type, interface or class. There are numbers of scenarios where we want to ensure typings on the module level. Scenario 1: Module Interface Many librar...
Open Graph Description: Introduction Currently, TypeScript allows to ensure typings on differents levels using type, interface or class. There are numbers of scenarios where we want to ensure typings on the module level. ...
X Description: Introduction Currently, TypeScript allows to ensure typings on differents levels using type, interface or class. There are numbers of scenarios where we want to ensure typings on the module level. ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/13231
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Proposal] External Module Type","articleBody":"## Introduction\r\n\r\nCurrently, TypeScript allows to ensure typings on differents levels using `type`, `interface` or `class`.\r\nThere are numbers of scenarios where we want to ensure typings on the `module` level.\r\n\r\n## Scenario 1: Module Interface\r\n\r\nMany libraries can be extended using plugins which are loaded as modules.\r\nThese libraries' maintainers could define an `interface` which should be implemented by the module.\r\n\r\n```ts\r\n// mylib.d.ts\r\nexport interface IPlugin {\r\n init(): void;\r\n build(context: PluginContext): boolean | PromiseLike\u003cboolean>;\r\n dispose?: () => void;\r\n}\r\n\r\n//plugin.ts\r\nimport { IPlugin, PluginContext } from \"mylib\";\r\n\r\nexport implements IPlugin;\r\n ^^^ ERR: Module does not implement \"IPlugin\". Missing members: \"init\".\r\n\r\nexport function build(context: PluginContext) { return false; }\r\n```\r\n\r\n## Scenario 2: Module proxy\r\n\r\nImagine a library which abstract RPC services. On the server part, you define services as modules and on the client part, you get Promised proxies of these services which you can use to remote call your service methods.\r\n\r\n```ts\r\n// myrpclib.d.ts\r\nexport type Deferred\u003cT> = { [P in keyof T]: Promise\u003cT[P]>; };\r\nexport function createClient\u003cT>(url: string): Deferred\u003cT>;\r\n\r\n// services/entity.service.ts\r\nexport function getAll(): Promise\u003cEntity[]> { /* ... */ }\r\nexport function getOne(id: string): Promise\u003cEntity> { /* ... */ }\r\nexport function create(entity: Entity): Promise\u003cEntity> { /* ... */ }\r\nexport function update(entity: Entity): Promise\u003cEntity> { /* ... */ }\r\nexport function remote(id: string): Promise\u003cvoid> { /* ... */ }\r\nexport function sync(id: string): Entity { /* ... */ }\r\n\r\n// clients/client1.ts\r\nimport { createClient } from \"myrpclib\";\r\ntype RemoteService = typeof module(\"../services/entity.service\");\r\nconst client = createClient\u003cRemoteService>(\"http://host/entity.service\");\r\n```\r\n\r\n## Scenario 3: Inline module typing\r\n\r\nBy extending the preceding scenario, we could imagine that the library also provides a module loader plugin for its managed services.\r\n\r\n```ts\r\n// myrpclib.d.ts\r\ndeclare module \"rpc!*\" { \r\n const res: any;\r\n export default res;\r\n}\r\n\r\n// clients/client2.ts\r\ntype RemoteService = typeof module(\"../services/entity.service\");\r\nimport client from \"rpc!host/entity.service\" as Deferred\u003cRemoteService>;\r\n```\r\n\r\n## Scenario 4: Wildcart module typing\r\n\r\nThis time, we have a library that proxy services in a `Web Worker`.\r\n\r\n```ts\r\n// myrpclib.d.ts\r\ndeclare module \"worker!*\" { \r\n const res: Deferred\u003ctypeof module(\"*\")>;\r\n export default res;\r\n}\r\n\r\n// clients/client3.ts\r\ntype RemoteService = typeof module(\"../services/entity.service\");\r\nimport client from \"worker!../services/entity.service\";\r\n\r\nclient.getAll().then(/* ... */);\r\nclient.sync().then(/* ... */);\r\n// client type is \"Deferred\u003ctypeof module(\"../services/entity.service\")>\";\r\n```\r\n\r\n## Language Features\r\n\r\n#### Syntatic\r\n\r\n**What is the grammar of this feature?**\r\n- [ ] `export implements IPlugin` _(could be written `implements IPlugin`)_\r\n- [ ] `typeof module(\"module/path\")`\r\n- [ ] `import myModule from \"myModule\" as Type`\r\n- [ ] `typeof module(\"*\")` _in wildcart module definition_\r\n\r\n**Are there any implications for JavaScript back-compat? If so, are they sufficiently mitigated?**\r\n- [x] No backward compatibility implications\r\n\r\n**Does this syntax interfere with ES6 or plausible ES7 changes?**\r\n- [x] No conflicting keywords\r\n\r\n#### Semantic\r\n\r\n**What is an error under the proposed feature? Show many examples of both errors and non-errors**\r\n```ts\r\n//export implements...\r\n// Same as an interface\r\n\r\n//typeof module(\"module/path\");\r\ntype Moduletype = typeof module(\"module/path\"); // OK\r\ntype UnknownModuleType = typeof module(\"unknown/module\"); \r\n ^^^^ // COMPILER ERROR: \"unknown/module\" does not exists\r\n\r\nconst test: typeof module(\"module/path\"); //OK\r\nfunction test(): typeof module(\"module/path\"); //OK\r\nfunction load(name: string): typeof module(name); //OK\r\n\r\n//import module as\r\nimport myModule from \"myModule\" as Type; //OK\r\nimport * as myModule from \"myModule\" as Type; //OK\r\nimport * as unknown from \"unknown\" as Type; //OK\r\nimport { someMember } from \"myModule\" as Type;\r\n ^^^^ // COMPILER ERROR: Only default and global import are allowed!\r\n\r\n//typeof module(\"*\")\r\ndeclare module \"proxy!*\" { \r\n const res: typeof module(\"*\"); // OK\r\n export default res;\r\n}\r\n\r\ntype myType = typeof module(\"*\"); \r\n ^^^^ // COMPILER ERROR: module \"*\" does not exists\r\n```\r\n\r\n**How does the feature impact subtype, supertype, identity, and assignability relationships?**\r\n- [ ] Moderate impact\r\n- [ ] Modules should be treated as special Interfaces (or special Classes) so they could be used in same contexts and they can interact we others.\r\n- [ ] An import could be typed\r\n\r\n**How does the feature interact with generics?**\r\n- [x] Limited impact\r\n- [x] If a module will become a `type` (like an Interface is), it should interact with generics.\r\n\r\n#### Emit \r\n**What are the effects of this feature on JavaScript emit? Be specific; show examples**\r\n- [x] No effect\r\n\r\n**Does this emit correctly in the presence of variables of type ‘any’? Features cannot rely on runtime type information**\r\n- [x] No emit difference\r\n\r\n**What are the impacts to declaration file (.d.ts) emit?**\r\n- [x] Limited impact\r\n- [ ] declaration file should contain `export implements...` informations\r\n\r\n**Does this feature play well with external modules?**\r\n- [x] Exclusively for external modules\r\n\r\n#### Compatibility \r\n\r\n**Is this a breaking change from the 1.0 compiler? Changes of this nature require strong justification**\r\n- [ ] New keywords are introduced but it is only typings utilities\r\n\r\n**Is this a breaking change from JavaScript behavior? TypeScript does not alter JavaScript expression semantics, so the answer here must be “no”**\r\n- [x] No\r\n\r\n**Is this an incompatible implementation of a future JavaScript (i.e. ES6/ES7/later) feature?**\r\n- [x] No\r\n\r\n#### Other \r\n\r\n**Can the feature be implemented without negatively affecting compiler performance?**\r\n- [x] Yes\r\n\r\n**What impact does it have on tooling scenarios, such as member completion and signature help in editors?**\r\n- [ ] Moderate impact\r\n\r\n## TODO\r\n- [ ] Get validation from the community\r\n- [ ] Get feedback from the community and improve spec\r\n- [ ] Add more exemples if needed\r\n- [ ] Implementation details","author":{"url":"https://github.com/SomaticIT","@type":"Person","name":"SomaticIT"},"datePublished":"2016-12-30T18:11:51Z","url":"https://github.com/microsoft/TypeScript/issues/13231"}
| 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:218223de-ac8d-2a4b-829b-2ba9482fb068 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A1C:363D8E:9884F8:C54A8B:6B00DE06 |
| html-safe-nonce | 122b9cce9a842447542d39c9f64b2f28f0ef4549d0c7e1305a1c5e0730bf11d4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTFDOjM2M0Q4RTo5ODg0Rjg6QzU0QThCOjZCMDBERTA2IiwidmlzaXRvcl9pZCI6IjU5ODgxNTE5OTg4MDg2MzIzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c09e22f14f21a8e47694e24717c720c35bcfc881ea97fa3c46a893abb8e8c82a |
| hovercard-subject-tag | issue:198178024 |
| 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/13231/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d5de74d9c0d3f4d344032ccc94b225473a9e8b394fcf85cb402aba3ba124347b/microsoft/TypeScript/issues/13231 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d5de74d9c0d3f4d344032ccc94b225473a9e8b394fcf85cb402aba3ba124347b/microsoft/TypeScript/issues/13231 |
| og:image:alt | Introduction Currently, TypeScript allows to ensure typings on differents levels using type, interface or class. There are numbers of scenarios where we want to ensure typings on the module level. ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SomaticIT |
| 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