Title: External module resolution for non-Typescript packages · Issue #2839 · microsoft/TypeScript · GitHub
Open Graph Title: External module resolution for non-Typescript packages · Issue #2839 · microsoft/TypeScript
X Title: External module resolution for non-Typescript packages · Issue #2839 · microsoft/TypeScript
Description: Good progress is made on supporting the case of packages that carry their own typings in #2338. That proposal doesn't currently deal with modules that do not carry their own typings, a.k.a. all modules that need typings from DefinitelyTy...
Open Graph Description: Good progress is made on supporting the case of packages that carry their own typings in #2338. That proposal doesn't currently deal with modules that do not carry their own typings, a.k.a. all mod...
X Description: Good progress is made on supporting the case of packages that carry their own typings in #2338. That proposal doesn't currently deal with modules that do not carry their own typings, a.k.a. all...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/2839
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"External module resolution for non-Typescript packages","articleBody":"Good progress is made on supporting the case of packages that carry their own typings in #2338.\n\nThat proposal doesn't currently deal with modules that do not carry their own typings, a.k.a. all modules that need typings from DefinitelyTyped.\n\nThis issue is to investigate possibilities of, and propose a (simple) solution for preventing problems when using (multiple versions of) external modules together.\n\n**TL;DR**\n1. No longer use `declare module \"...\" { }` in typings for external modules\n2. Add an extra step to the resolution logic of #2338 to look up this type of external typings\n\n**Example scenario**\n- myprogram\n - mylib\n - myutils@1.0\n - myotherlib\n - myutils@2.0\n\nSuppose that both `mylib` and `myotherlib` export a function, that returns a value of a type defined in `myutils`.\n\n**Existing scenario**\n\nLikely, both `mylib` and `myotherlib` will each have a `/// \u003creference path=\"./typings/tsd.d.ts\" />` line, which again has a `/// \u003creference path=\"./myutils/myutils.d.ts\" />` line.\n\n**Problem**\n\nBoth libs carry their own version of `myutils.d.ts`, which works fine as long as they are not used together.\n\nHowever, when they _are_ used together (as in this example), all the `/// \u003creference ...>` lines will (need to) be 'merged', which essentially means the `declare module \"myutils\" { ... }` from both `mylib` and `myotherlib` will end up in the same 'global module name namespace'.\n\nThis gives a compiler error, and even if the compiler would somehow allow it, would make it impossible to refer to either version of `myutils`.\n\nAdditionally, tools like [`dts-generator`](https://github.com/SitePen/dts-generator) e.g. include the `/// \u003creference>` line inside of the `declare` blocks, which get ignored by the compiler. This leads to unknown-references when trying to use such a typing, and will typically be solved by adding e.g. a `myutils/myutils.d.ts` to `myprogram/typings/tsd.d.ts`, which will be wrong for one of the libs.\n\n**Solution**\n\nExternal modules have the nice property that there is no 'global namespace' (except the filesystem, maybe), and basically every reference is 'local'.\n\nIt makes sense to use the same referencing scheme that's used for resolving 'local' external modules (within a package) for 'normal' external modules (different npm packages).\n\nBasically, the idea consists of:\n- No longer using `declare module \"...\" { }` in typings for external modules (to prevent the global namespace clash)\n- Adding an extra step to the resolution logic of #2338 to look up this type of external typings (e.g. in `typings/` folder)\n\n**Advantages**\n- Multiple versions of a package can co-exist in the same program/library\n- `/// \u003creference>` lines are no longer needed in most cases (except probably for e.g. importing Mocha's `it()` and other 'true' globals)\n- One format to rule them all: external module typings are already generated without `declare module \"...\" { }` by `tsc`, so all 'sorts' of external module typings will look the same\n- Gradual upgrade path: the `tsd.d.ts`-way will still work for packages that do still use `declare module \"...\" { }` (although it will still have the name-clash problem etc.)\n\n**Disadvantages**\n- Typings on e.g. DefinitelyTyped will (gradually) need to be replaced with their non-wrapped version\n- Extra lookup step in the compiler\n- Location of these external typings (a.k.a. include path) needs to be configured somewhere and/or sensible default value chosen\n\n**Example 'new-style' typings**\n\nMost hand-crafted typings will typically look like:\n\n``` ts\n/// myutils.d.ts\nexport interface BarType {\n bar: number;\n}\nexport declare function something(): BarType;\n```\n\nBut just to make things more interesting, suppose the typings of `myutils` look like:\n\n``` ts\n/// dist/bar.d.ts\nexport interface BarType {\n bar: number;\n}\n\n/// dist/foo.d.ts\nimport bar = require(\"./bar\");\nexport declare function something(): bar.BarType;\n```\n\nNothing special here, this is what `tsc` generates today.\n(Note: dist/ may be because someone used CoffeeScript, not TypeScript, right? :))\n\nNow, the typings dir on e.g. DefinitelyTyped could look like:\n\n```\nDefinitelyTyped/myutils/latest/dist/bar.d.ts\nDefinitelyTyped/myutils/latest/dist/foo.d.ts\nDefinitelyTyped/myutils/1.0/dist/bar.d.ts\nDefinitelyTyped/myutils/1.0/dist/foo.d.ts\n```\n\n(Or maybe `2.0` instead of `latest`, ideas welcome.)\n\nAnd both `mylib` and `myotherlib` would have:\n\n```\ntypings/myutils/dist/bar.d.ts\ntypings/myutils/dist/foo.d.ts\n```\n\n**Looking up .d.ts**\n\nGiven e.g. `import { something } from \"myutils\"` the compiler could use [`myutils/package.json`'s `main` property](https://github.com/Microsoft/TypeScript/issues/2338#issuecomment-94555288) to get to `./dist/foo.js`, which in turn would resolve to `dist/foo.d.ts`.\n\nNice and simple: no need to have support from the authors of `myutils`, no `/// \u003creference>` lines.\nAnd it even supports the `import bar = require(\"myutils/dist/bar\")` case.\n\n**Open questions**\n- Use one typings dir for both 'wrapped' and 'unwrapped' typings?\n- First do a lookup for external modules already `declare`'ed, e.g. through tsd.d.ts? If so, would probably make first bullet usable, nice for gradual upgrade path of DT typings.\n- Most DT typings will not want to mimic exact filesystem structure of package, using e.g. `myutils/latest/index.d.ts` will probably work out-of-the-box with external module resolution logic, but `myutils/latest/myutils.d.ts` or even `myutils/myutils.d.ts` may be easier for filename in editor tab?\n\nGood idea? Bad idea?\n","author":{"url":"https://github.com/poelstra","@type":"Person","name":"poelstra"},"datePublished":"2015-04-20T21:54:53Z","url":"https://github.com/microsoft/TypeScript/issues/2839"}
| 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:7c48537e-60d1-a732-d380-055f42ce374f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | ABC2:63875:D58933:11BCCC9:6B00FB79 |
| html-safe-nonce | 4cc2b7f0edfc9151e9694bfd7453fbca0e245ea51b5484e1bb0e40dadd050e63 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQkMyOjYzODc1OkQ1ODkzMzoxMUJDQ0M5OjZCMDBGQjc5IiwidmlzaXRvcl9pZCI6IjEwNzcxODQxMjkzOTA5OTAwMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 341319337399fdbb4317efb9be86dad77134efbbeca6eade48a7300118b2d9ca |
| hovercard-subject-tag | issue:69690167 |
| 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/2839/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d8eba66cd3ae611807b8ad0b217eab6c4d8ac453323289ccc4777d7e5a6432ac/microsoft/TypeScript/issues/2839 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d8eba66cd3ae611807b8ad0b217eab6c4d8ac453323289ccc4777d7e5a6432ac/microsoft/TypeScript/issues/2839 |
| og:image:alt | Good progress is made on supporting the case of packages that carry their own typings in #2338. That proposal doesn't currently deal with modules that do not carry their own typings, a.k.a. all mod... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | poelstra |
| hostname | github.com |
| expected-hostname | github.com |
| None | e208d86428ec00a57ade75058166297440e8128a01e5287c31a53dc042bbf187 |
| 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 | 7dafd017fb7809a839c443dba08622af38e63c09 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width