Skip to content
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

Make DOM types available without polluting the global scope with 790 global variables #1734

Open
SystemParadox opened this issue Jun 4, 2024 · 3 comments

Comments

@SystemParadox
Copy link

Adding lib: ['DOM'] introduces 790 global variables. Many of these are deprecated and useless. Many of them have short names that can hide mistakes like a missing var or let. Some obvious offenders that absolutely result in regular bugs:

  • event
  • closed
  • parent
  • screen
  • status
  • toolbar
  • top
  • length
  • self
  • history
  • location
  • origin

For this reason I have so far refrained from using lib: ['DOM'] in my projects and prefer to access all globals via window.foo instead for clarity and safety.

However, this means that TS doesn't know about any of these types. Please can we make these types available so that we can at least access and use the type information without polluting the global scope with all these variables.

I'd be happy with one that just declares document and window globals, or a types-only version that leave all globals up to us, either way I don't mind, just anything that makes the types available without all these junk globals.

@HolgerJeromin
Copy link
Contributor

ref microsoft/TypeScript#14306 and microsoft/TypeScript#18433
Often "fixed" with a eslint rule no-restricted-globals

@SystemParadox
Copy link
Author

If eslint had a way to whitelist only a specific set of acceptable globals that would be ok but having to blacklisting them all with no-restricted-globals is madness.

@HolgerJeromin
Copy link
Contributor

Far from elegant but configuring eslint once is far from madness IMO.

eslint rule
'no-restricted-globals': [
    'error', // eslint severity
    // globals similar to local variables
    'error',
    'event',
    'external',
    'location',
    'name',
    'orientation',
    'toString',
    // globals similar to local element properties
    'addEventListener',
    'blur',
    'close',
    'closed',
    'dispatchEvent',
    'focus',
    'innerHeight',
    'innerWidth',
    'length',
    'moveBy',
    'moveTo',
    'onclick',
    'open',
    'origin',
    'outerHeight',
    'outerWidth',
    'parent',
    'removeEventListener',
    'resizeBy',
    'resizeTo',
    'scroll',
    'scrollBy',
    'scrollTo',
    'scrollX',
    'scrollY',
    'status',
    'stop',
    'top',
],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants