-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Support custom/non-vendor modules for lib
#45959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This can already be controlled with the "types": ["custom-lib-module"] |
That does work too, but using custom-lib-module/tsconfig.json{
"compilerOptions": {
"types": ["./types"]
}
} consumer/tsconfig.json{
"extends": "custom-lib-module/tsconfig.json"
"compilerOptions": {
"types": [
// Now we need to explicitly specify any @types modules installed.
// But all we really wanted was a lib setup.
]
}
} |
You could just |
You can, yes, but this requires per-file inclusion, which is much less convenient than a project-level option. |
A couple more thoughts regarding this approach. If this were supported, perhaps the package.json resolution step in #45771 would be superfluous. Instead, tsconfig might include As an additional use case side note, I'm also thinking of the utility here for editor tooling for those less familiar with TS. If someone can get TS's autocompletion, hints etc with a standard shared config (potentially preconfigured in a domain-specific IDE), that would be nice. That's another mark against triple-slash directives. |
No—the reason the package.json thing is important is that you need every existing reference to |
Ah, right! Yes, that makes sense. |
Anyway, to your point about not liking |
In my experience, custom-lib-module/tsconfig.json{
"include": ["./lib"],
// other compiler options etc.
} consumer/tsconfig.json{
"extends": "custom-lib-module/tsconfig.json",
"include": [
"src/**/*"
// and also whatever is in custom lib’s "include"
]
} |
I’m not trying to be difficult here — I do get that this is kind of supported and I’m just pushing for a slightly cleaner/simpler/more shareable way of doing this, so I totally get if there’s no bandwidth for something like this. |
I think this can just be solved pretty well with the current tools. We ship a bunch of 'import redirect'-y files in lib which will never change:
If you shipped:
All you would need is:
Then it would correctly override just the ES5 wrapped file, not touch any of the actual imports and add your own in the process as globals in any project (with that dependency set up) Though I can empathize with the idea that there could be a lookup for |
OK, cool, that‘s kind of what I meant by “squatting” on a standard lib name, but reframed like this, perhaps that’s actually an intended rather than hacky use — you’re overriding/augmenting an existing lib, so that does fall into the #45771 use case. The fact this requires two configuration points across separate files that need to be kept in sync does still introduce more friction than I’d like, and the fact that tsconfig doesn’t clearly show the actual lib in use may be a source of confusion, but you can’t have everything in life 😃. Thanks for taking the time to consider this anyway! |
Suggestion
🔍 Search Terms
lib
custom
environment
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
If I understand correctly, currently the
lib
option intsconfig
can refer to a list of built-in lib declarations to configure the expected JavaScript environment. #44795 and #45685 (implemented via #45771) seem to partially allow customisation of this by pointing to a node module to override a given lib file, e.g.tsconfig.json
package.json
In some environments it would be helpful to be able to provide a non-standard lib file to extend what is available globally.
Would it be possible to support passing these directly to
lib
and have them resolved via the usual package dependency route? In this case,custom-lib-module
would be a node module of the appropriate format.The additional hurdle beyond the module resolution, which #45771 added, is that this would allow values in
lib
other than those in the list of values supported currently.📃 Motivating Example
This change makes supporting custom JavaScript environments easier, by allowing easy sharing and extending of
lib
configurations.💻 Use Cases
I write JavaScript for use inside Cycling 74’s Max. This runs scripts in an environment with a significant number of custom global APIs (I guess comparable to how a browser environment extends the base ECMA standard). For lesser used environments like this, it would be unreasonable to expect TypeScript to provide built-in lib definitions (or even be aware of them), but being able to write, share, and consume custom
lib
modules like this would be really helpful.Current workarounds include:
I guess if Support resolving
@typescript/[lib]
in node modules #45771 is released in 4.5, it would be possible to hack around this by squatting on one of the official lib names, adding something like"@typescript/dom": "npm:custom-lib-module"
, but that seems suboptimal. (But — from a position of supreme ignorance — maybe points to a fairly easy path for implementing this?)Including the custom lib declaration files in
tsconfig.include
. This has the drawback of forcing use ofinclude
, which can complicate some set-ups and is not ideal intsconfig
files that are intended to be shared across projects.Using a triple-slash directive. This requires per-file inclusion, which is much less convenient than a project-level option.
The text was updated successfully, but these errors were encountered: