Title: Generic namespaces · Issue #19728 · microsoft/TypeScript · GitHub
Open Graph Title: Generic namespaces · Issue #19728 · microsoft/TypeScript
X Title: Generic namespaces · Issue #19728 · microsoft/TypeScript
Description: REVISED: My initial proposal was wrong, here is a revised version. I have a library that exports a collection of interrelated interfaces, classes, and functions, which operate collaboratively on a single generic type. For instance, you c...
Open Graph Description: REVISED: My initial proposal was wrong, here is a revised version. I have a library that exports a collection of interrelated interfaces, classes, and functions, which operate collaboratively on a ...
X Description: REVISED: My initial proposal was wrong, here is a revised version. I have a library that exports a collection of interrelated interfaces, classes, and functions, which operate collaboratively on a ...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/19728
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Generic namespaces","articleBody":"**REVISED: My initial proposal was wrong, here is a revised version.**\r\n\r\nI have a library that exports a collection of interrelated interfaces, classes, and functions, which operate collaboratively on a single generic type. For instance, you can use them to perform operations on bot messages, game actions, or IoT events, but typically you'd only ever be working with one of those at a time. So the fact that the library is generic gives it broad appeal to a wide variety of developers, but is not itself of value to a given developer. (Contrast with RxJS where solid generic support has specific value to a given developer, because they will likely use it with a wide variety of types in a given project.)\r\n\r\nAn unfortunate consequence of the way the components of this library come together is that they are sometimes very resistant to type inference, so many invocations end up requiring a type decoration, which creates an enormous amount of visual noise. It's a terrible developer experience.\r\n\r\nWhat I have now is:\r\n\r\n```ts\r\n// Library\r\nexport class Class\u003cT> {\r\n}\r\nexport function func\u003cT>(..) {\r\n}\r\n\r\n// Library Consume\r\nimport { Class, Interface, func } from 'my-library';\r\n\r\n// frequently need to spell out Class\u003cBot>, Interface\u003cBot>, and func\u003cBot>\r\n```\r\n\r\nType aliases help a little, because I can do:\r\n\r\n```ts\r\ntype BotInterface = Interface\u003cBot>;\r\n```\r\n\r\nBut there is no equivalent for functions and classes. And even if there were, that would still make it painful to write my library, because it would be using generics everywhere when what I really want to do is write my library with just a single top-level generic. And it would still make it overly painful to consume my library, because aliases would have to be manually created for every component in the library. What I want is a *generic namespace*:\r\n\r\n```ts\r\n// Library\r\nexport namespace\u003cT> {\r\n export class Class {\r\n }\r\n export function func(...) {\r\n }\r\n}\r\n\r\n// Library Consumer\r\nimport { MyNamespace } from 'my-library';\r\nconst { Class, Interface, func } = MyNamespace\u003cBot>;\r\n\r\n// just use Class, Interface, and func\r\n```\r\n\r\nWorkarounds: I tried mimicking this by creating a generic factory class, but it is a huge hack and not powerful enough. There is a good reason namespaces exist in TypeScript, and adding generics would make them much more powerful. As it stands, the only real workaround -- which I have been forced to take, and is obviously unsustainable and unmaintainable -- is to fork the library into one version for each type.","author":{"url":"https://github.com/billba","@type":"Person","name":"billba"},"datePublished":"2017-11-03T22:22:35Z","url":"https://github.com/microsoft/TypeScript/issues/19728"}
| 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:502f3968-6654-a76f-9af1-0bdf1caa8016 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D99A:3DD488:691DE2:873763:6B00E175 |
| html-safe-nonce | fc5b33b8d368736b3e6a32e51b1e0511bae2d29906e1a3e847785ba34fa2f3f5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTlBOjNERDQ4ODo2OTFERTI6ODczNzYzOjZCMDBFMTc1IiwidmlzaXRvcl9pZCI6IjUyNDY1NzgyNjM5NjY5Mjg1MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4510fd9332e8fcc63f9043f9ab9194172baba11f5b5278699a45feb8b899bd7c |
| hovercard-subject-tag | issue:271134315 |
| 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/19728/issue_layout |
| twitter:image | https://opengraph.githubassets.com/70f223ec158e1d71d1e34896adf8a52bd11bb4cf12548d7a2b3431bc91417039/microsoft/TypeScript/issues/19728 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/70f223ec158e1d71d1e34896adf8a52bd11bb4cf12548d7a2b3431bc91417039/microsoft/TypeScript/issues/19728 |
| og:image:alt | REVISED: My initial proposal was wrong, here is a revised version. I have a library that exports a collection of interrelated interfaces, classes, and functions, which operate collaboratively on a ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | billba |
| 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