-
Notifications
You must be signed in to change notification settings - Fork 12.8k
compiler option to enforce that declare global
or declare module
only happens when those files are imported.
#42003
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 isn’t true generally. When you import a package root we don’t crawl all the files/declarations in that package, we just go to the I guess the only places this proposal could have an effect is in files that were included for a reason other than a transitive reference from an root file, which would be Am I totally misunderstanding you? It seems like what you’re proposing is already the way it works. |
@andrewbranch I think that may be where my confusion comes from: that a library has I have to experiment a bit more to reproduce the behavior I saw, this time paying attention to |
On a similar note, I had types: [],
typeRoots: [] in tsconfig.json, and I was not importing any files that indirectly imported files with If I understand correctly, based on what you said, I can import one specific file from a package (a file that does not import any global types, indirectly or not), but if the project has a If so, that's most likely the problem I have, and the reason for the issue, and empty |
No, if you import a specific file from a package, we only process that file and the things it imports. The |
Alright, then I'll close this for now, as there is no need for an option. I must have overlooked something in my case. |
I think this would be a very powerful option that would solve issues like #37053 (in particular see the list of related at the bottom of the comment #37053 (comment)).
Suggestion
A new option, like
declareOnImportOnly
would be amazing!Use Cases
Given a file like
If the user installs
somelib
, but they never import thesomelib/FooBar.ts
file (orsomelib/FooBar.{js/d.ts}
if the package is compiled), they will still have the definition of'foo-bar': FooBar
.If the user writes the following code, they will not see a type error, but they will have an unexpected runtime error:
At runtime, the user will get an error like
cannot call 'undefined'
because thefoo-bar
element was never defined, because that file was neverimport
ed, thereforeel.foo
is not a function becauseel
is an instance ofHTMLElement
and not an instance ofFooBar
.For the users code to work, they need to import the file (directly or indirectly):
For all the problems listed in #37053, the solution is simple: with the new
declareOnImportOnly
option set totrue
, you must simplyimport
whatever you need.This would be great, because the purpose of
import
syntax is fulfilled: now user useimport
for importing... anything they need!No longer will unexpected types appear in scope (or at least, it will be minimized, because if a user imports a package's index file, they may end up importing all the types that they didn't want, but at least they won't get runtime errors because the runtime modules will have also been imported).
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: