-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow types from lib.dom to be imported explicitly via module #46754
Comments
These imports would be preserved and would consequently fail at runtime. This is not possible.
Those files should ideally be contained in a separate tsconfig that sets the appropriate libs.
Me too, but this is a JS/DOM problem and not something TypeScript can solve. |
I see what you're saying. I'm sure you can understand how unergonomic this is from a development perspective, also we don't have a built-in type library that covers these environments
Yeah, you're right. Perhaps this is the role of an external type library and not a responsibility of the TypeScript project itself. |
should do the trick |
I asked for a very similar feature over at microsoft/TypeScript-DOM-lib-generator#1207 and it's still open. I'm not sure I actually need it anymore, though... |
The problem with a pretend module that exports only types is that we have no concept of a type-only module. By declaring a module named |
Isn't it a consequence of the very idea of |
Inability to do this actually breaks the code at runtime when using Since we can't import the definitions, we need to reference the whole lib, which wrongly sets the globals which never exist in NodeJS and thus breaks the code at runtime. |
Exactly right. |
tldr
Allow for the import of libdom types explictly
I am not talking about built-in language features like
Array.filter
,Proxy
, or similar. I'm specifically referring to environment specific functionality likeMessageChannel
,Fetch
,Location
, etcJustification
When consuming builtins like libdom, the entirety of the dom types are included into the global scope (exactly as they are when running JS in the browser).
My issue is, in larger applications or mono repos, having ambient types can cause naming collisions and ambiguous functionality.
Shadowed global variable names
The removal of shadowed global variables will not cause a type error as expected. As an example, if you name a variable
name
Then you later delete
name
Any logic further down that depends on it will still work because it assumes you're referring to
window.name
.Unexpected global overwrites
This can also cause issues where certain libraries will override the globals.
For instance if you have a mono repo with a backend and frontend together, the definition for
setTimeout
is different in@types/node
than it is indom
and there is a collision.If you install
styled-components
(or have another package using React Native) then they will use the globals for React Native which overridefetch
andHeaders
.Incorrect context specific globals
What if the file I am working in is a Web Worker or Service Worker? These contexts don't have all the functionality of libdom but are offered global types that indicate that they do.
Dependency injection
Lastly, explicit imports are better when it comes to reasoning about dependency injection.
Suggestion
I would love to be able to import global dependencies directly
example:
The text was updated successfully, but these errors were encountered: