question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support having '@types/web' as overriding 'dom' in TypeScript's lib resolver

See original GitHub issue

Suggestion

I’d like to be able to install @types/web and have the following happen:

  • Other dependencies (like from DT) using something like /// <reference lib="dom"> do not resolve to the vendored libdom.d.ts
  • The default inclusion of dom in a project via lib would automatically pick up @types/web (or be fully suppressed)

🔍 Search Terms

dom types/web web lib

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • x ] This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

⭐ Suggestion

  1. Hardcode some sort of dom -> @types/web inside the TS compiler’s
  2. Support some kind of pragma in @types/web

1 - Hardcoding

Pros: @types/web is a special case (though there could be more in the future) and this could be the smallest possible change.

Cons: It feels a bit meh.

2 - Pragmas

Pros: We have existing pragmas: /// <reference no-default-lib="true"/> for type of thing. This answer is totally generic and would allow others to have their own implementations of other deployed libs.

Cons: I’d need to check, but this might mean looking for the pragmas across all @types dts files before we can reliably use the files inlib

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
ortacommented, Sep 17, 2021

#45771 shipped this into the TypeScript 4.5 - the answer was to leave dependency managers to handle the resolution

This answer to supporting your own versions of in-built libraries uses the node_modules resolution structure to let you define libraries. The path lookup looks like:

  • lib.dom.d.ts -> @typescript/dom
  • lib.dom.iterable.d.ts -> @typescript/dom/iterable
  • lib.es2015.symbol.wellknown.d.ts -> @typescript/es2015/symbol-wellknown

If you made a package which just sets up @typescript/es2015/symbol-wellknown.d.ts without @typescript/es2015/index.d.ts, then es2015 would still resolve to the TypeScript hosted version.

All JS dependency managers support this syntax:

{
  "dependencies": {
    "@typescript/dom": "npm:@types/web"
  }
}
3reactions
MartinJohnscommented, Jun 29, 2021

With the pragma approach, what would happen if two packages aim to replace the definitions? This could easily happen with unfortunate dependencies.

A third option may be to have it explicitly configured in the tsconfig.json. I’m the last person who’d ask for yet another flag / option, but it sounds sensible for this. This would allow replacement of any library file, and it’s explicit (no magic).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - TypeScript 4.5
TypeScript 4.5 introduces a way to override a specific built-in lib in a manner similar to how @types/ support works. When deciding which...
Read more >
What's new in TypeScript 4.5 - LogRocket Blog
TypeScript 4.5 includes enhanced Awaited type, supported lib from ... @types/web represents TypeScript's published versions of the DOM APIs.
Read more >
Announcing TypeScript 4.5 Beta - Microsoft Developer Blogs
ECMAScript Module Support in Node.js; Supporting lib from ... today TypeScript publishes versions of the DOM APIs on @types/web .
Read more >
Understanding Advanced TypeScript Concept & Types
Know what are advanced typescript concepts, type assertions & aliases ... to @types/ support allows you to override a specific built-in lib.
Read more >
typescript - How can I override a variable's type which has ...
According to Titian Cernicova-Dragomir, I can't achieve this without modifying TypeScript's built-in lib.dom.d.ts .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found