Title: Proposal for correct version lookup in deeply embedded non-TS external modules · Issue #4673 · microsoft/TypeScript · GitHub
Open Graph Title: Proposal for correct version lookup in deeply embedded non-TS external modules · Issue #4673 · microsoft/TypeScript
X Title: Proposal for correct version lookup in deeply embedded non-TS external modules · Issue #4673 · microsoft/TypeScript
Description: Update 20150908: applied feedback from comments. Thanks James Brantly (@jbrantly)! Also renamed "Legacy mode" to "Mixed mode" and clarified what it's about. Update 20150909: more feedback from James Brantly (@jbrantly). Now that #2338 is...
Open Graph Description: Update 20150908: applied feedback from comments. Thanks James Brantly (@jbrantly)! Also renamed "Legacy mode" to "Mixed mode" and clarified what it's about. Update 20150909: more feedback from Jame...
X Description: Update 20150908: applied feedback from comments. Thanks James Brantly (@jbrantly)! Also renamed "Legacy mode" to "Mixed mode" and clarified what it's about. Update 20150909:...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/4673
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Proposal for correct version lookup in deeply embedded non-TS external modules","articleBody":"Update 20150908: applied feedback from comments. Thanks @jbrantly! Also renamed \"Legacy mode\" to \"Mixed mode\" and clarified what it's about.\nUpdate 20150909: more feedback from @jbrantly.\n\nNow that #2338 is implemented, we have nice resolution of typings for 'native TS modules'.\nHowever, exporting typings for non-TS modules can be problematic in case of deeper dependency trees.\n\nIssue #2839 (by me) contains a proposal to solve this, but does not talk about deeper-than-one dependencies in case of conflicting versions. Issue #4665 (by @mhegazy) also addresses non-TS resolution, but does not explicitly mention the lookup logic.\n## Example dependency tree\n- myprogram (native TS module)\n - **mylib** (native TS module, with 'proper external' typing)\n - foolib@1.0 (plain JS module)\n - **utils@3.0** (plain JS module)\n - barlib@1.0 (plain JS module)\n - **utils@4.0** (plain JS module)\n - myotherlib (native TS module, with 'proper external' typing)\n - foolib@2.0 (plain JS module)\n - utils@4.0 (plain JS module)\n\nAssumptions about this tree:\n- Mainly talking about NodeJS (CommonJS) environment here, not trying to solve the bower problem\n- None of the JS modules pollute the global namespace, i.e. they are 'normal' CommonJS modules\n## Problem description\n\nIn this case, because `mylib` is the 'first' package that knows about TS, it somehow needs to provide the typings for all non-TS stuff it exposes: `foolib@1.0`, `barlib@1.0` but also both `utils@3.0` **and** `utils@4.0`.\n\nIn an ideal world, using 'proper external modules' (see #4665 and #2338) would solve this if they were all TS modules (i.e. they provide their own typings in their npm package).\n\nIn this case though, the `import` statements need to resolve to e.g. DefinitelyTyped typings, typically located in e.g. `mylib/typings/`.\n\nSo, `mylib/typings/` needs to have a `utils.d.ts` for version 3.0 and 4.0, and the compiler needs to know how to find them, and which version to use.\n\nEspecially note the difference between the concept of the current JS module versus current TS module in the following proposal.\n## Proposed algorithm\n\nNaming (taken from #4665):\n- 'ambient external' module typing means it contains `declare module \"Y\" { ... }` declarations\n- 'proper external' module typings don't have `declare module \"Y\" { ... }`, but directly export their classes, variables, etc.\n\nWhen compiling a package `X`, and looking for typings of an external module `Y/Z`, let:\n- `CurrentTSModule` = X\n- `CurrentJSModule` = X\n- `Z` = `\"index\"` if module is imported without a path (i.e. `import \"Y\"` instead of `import \"Y/Z\"`)\n\nNow:\n- Use the logic of #2338 to locate `Y`'s `package.json (starting at`CurrentJSModule`)\n - i.e. also traversing `node_modules` of parent packages\n- If `Y` provides its own typing (either `index.d.ts` or by following `typings` property in `package.json` in the package directory)\n - Parse that typing as a 'proper external module' typing\n - Set `CurrentTSModule` and `CurrentJSModule` variables to `Y`, i.e. any external modules used by `Y` should be resolved by looking in `\u003cY>/node_modules` and `\u003cY>/typings/`, no longer in `\u003cX>/typings/`\n - Done\n- Otherwise, search for typings using either:\n - ALGORITHM A: searches for `Y` in `X`'s typings folder (let's call it `typings/`):\n - `typings/\u003cY>@\u003cmajorY.minorY.patchY>/\u003cZ>.d.ts` (proper mode)\n - `typings/\u003cY>@\u003cmajorY.minorY>/\u003cZ>.d.ts` (proper mode)\n - `typings/\u003cY>@\u003cmajorY>/\u003cZ>.d.ts` (proper mode)\n - `typings/\u003cY>/\u003cZ>.d.ts` (proper mode)\n - `typings/\u003cY>/\u003cY>.d.ts` (mixed mode)\n - `typings/\u003cY>.d.ts` (mixed mode)\n - ALGORITHM B: match against .d.ts files with `\u003clibrary ... />` tags\n - For all .d.ts files passed on commandline/tsconfig.json that have a `\u003clibrary ... />` tag, match `Y`'s `package.json` name and version against the name and semver specification in the `\u003clibrary>` tag\n- When a match is found:\n - Keep `CurrentTSModule` as `X`, but set `CurrentJSModule` to `Y`\n - This ensures that the correct version of any sub-package is found\n - If 'proper mode': parse as 'proper external module', done\n - If 'mixed mode': determine whether it's a 'proper external' or 'ambient external' typing:\n - If 'proper external': use it as-is, done\n - If 'ambient external':\n - Don't expose any ambient declarations (modules, namespaces, variables, types), nor recursive\n `\u003creference>`'d declarations, to the global module namespace, and\n - Look for the `declare module \"Y/Z\" { ... }` and use its contents as the result (i.e. 'convert to proper external')\n\nNotes:\n- `\u003cmajorY>` etc. are based on the version as found in `Y`'s `package.json`\n- In contrast to the lookup in `node_modules`, lookups in `typings` do _not_ traverse `typings/` dirs of parent packages. _Only_ those of (TS-)package `X` are searched.\n- This algorithm correctly determines that `foolib` uses `utils@3.0`, but `barlib` uses `utils@4.0` (i.e. `CurrentTSModule` stays `mylib`, but `CurrentJSModule` switches from `foolib` to `barlib`)\n## Mixed mode\n\nMixed mode (previously called \"legacy mode\"), is intended to allow existing DefinitelyTyped typings\n(which use 'ambient external' scheme) to basically be used as external modules (CommonJS) without making a lot of 'accidental' globals available (e.g. the `Promise` type in `bluebird` typings), while still also allowing them to be used in AMD and plain script modes ('isomorphic typings').\n\nThese isomorphic typings typically declare lots of things as globals (perfect for plain script mode),\nbut these are usually not made globally available when loaded as CommonJS.\n\nSo, the idea is to wrap the whole typing into its own private space, then only make the actually requested external module part of it available.\n\nNote that if an isomorphic typing includes a 'proper external' typing, and the proper external typing\n`\u003creference>`'s another typing, that typing is still allowed to declare globals (they will not be 'isolated').\n\nHaving two packages declare the same global (e.g. when a proper external typing references a .d.ts, which explicitly marks a variable, class, etc as being globally available) currently leads to a compiler error (\"Duplicate identifier\").\nOne idea I had was to 'merge' the types of such globals instead (e.g. if two packages both declare a `Promise`, but they are in fact of different types, the resulting global will be typed as e.g. `declare var Promise: PromiseType1|PromiseType2;`\nThis way, the compiler can error when the global is actually _used_ (as opposed to when declared) in an incompatible way (see https://github.com/Microsoft/TypeScript/issues/4673#issuecomment-138859595)..\n## Discussion items\n- I'm not sure whether I prefer Algorithm A or B, yet.\n Algorithm A has the advantage of being 'filesystem based', just like node's environment.\n But B supports more complex semver matches and prevents bikeshedding over e.g. the name and structure of `typings/`.\n- When using Algorithm A (files), @jbrantly suggested to have an extra check for ES6 typings, i.e. preferring e.g. `typings/\u003cY>.es6.d.ts` over `typings/\u003cY>.d.ts` when available. Or possibly `typings/\u003cY>.\u003ctarget>.d.ts` where `\u003ctarget>` is the `--target` passed to tsc. See comments below for discussions on pros/cons.\n- How to 'override' the type a global actually will have, in case of conflicting types (see https://github.com/Microsoft/TypeScript/issues/4673#issuecomment-138859595)\n","author":{"url":"https://github.com/poelstra","@type":"Person","name":"poelstra"},"datePublished":"2015-09-06T16:06:24Z","url":"https://github.com/microsoft/TypeScript/issues/4673"}
| 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:2c389b52-43a3-e660-c8e6-cccb0a6ae024 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 81E8:2C4BED:BC4DF6:F0998E:6B00FB79 |
| html-safe-nonce | 4855daa4eac092e42dded8ce0c77890c5fa4d7574f4b5b151f9d69734ff43f6f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MUU4OjJDNEJFRDpCQzRERjY6RjA5OThFOjZCMDBGQjc5IiwidmlzaXRvcl9pZCI6Ijg4MDc1MDI1MzE2MTA2MDY0NTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a511e0b5aaf44b122036f4d7e2652f4736ce56bf622c7f496c44fa2912d7f0bd |
| hovercard-subject-tag | issue:105101612 |
| 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/4673/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c2df2bf58d1da1562ec82696f033771d3760af1c3cec09eb3836285761e1db4b/microsoft/TypeScript/issues/4673 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c2df2bf58d1da1562ec82696f033771d3760af1c3cec09eb3836285761e1db4b/microsoft/TypeScript/issues/4673 |
| og:image:alt | Update 20150908: applied feedback from comments. Thanks James Brantly (@jbrantly)! Also renamed "Legacy mode" to "Mixed mode" and clarified what it's about. Update 20150909: more feedback from Jame... |
| 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