Respect X-TypeScript-Types header for remote imports #38864
Labels
Out of Scope
This idea sits outside of the TypeScript language design constraints
Suggestion
An idea for TypeScript
Search Terms
Suggestion
The TypeScript compiler could respect the
X-TypeScript-Types
header when pointing to a remote import.For example:
Right now, the compiler will be unable to resolve the module path and I won't get any type safety, despite the fact that the HTTP response comes with a
X-TypeScript-Types
header that can be used to find the declarations for that module.The header has been implemented in Pika in anticipation of Deno (and similar work is being done in UNPKG unpkg/unpkg#243), but I think it would be great if TypeScript itself would download, cache and resolve the declarations too.
Use Cases
Lots of my projects use UNPKG/Pika directly and they're written and shipped in modern JS as ES Modules. I use TypeScript declaration files and the JSDoc syntax extensively to maintain type safety without introducing a source transform step.
If I want type safety for third party modules, I have three options.
paths
to alias the remote url to the local declarationsdeclare module "https://..." { export * from "..." }
(this doesn't always work depending on how the module is authored)The options that involve downloading the types through npm also require additional overhead in ensuring that the versions are matching at all times.
Examples
This would be used to write TypeScript without relying on npm. It would also help reduce friction with the Deno community.
Here's a rough sketch of the expected behaviour.
https://cdn.pika.dev/[email protected]
any
.X-TypeScript-Types
header with a value of/-/[email protected]/dist=es2019,mode=types/index.d.ts
I would recommend that if implemented, this feature would be locked behind a compiler option. Having the compiler access the network would be an unwelcome surprise if you weren't expecting it.
I also appreciate that putting a networking component into the compiler in the first place might be considered too complex to justify what is an uncommon use case right now, but as support for ES Modules continues to improve and more developers start to use the Deno toolchain, I think we'll see remote imports become far more common. It would be nice if they 'just worked' in TypeScript too.
I also think this could be implemented as a proof of concept in a third party CLI tool as a first step. Eventually I'd prefer to see it as out of the box behaviour in TypeScript itself, but having a
fetch-remote-types
command that did the same process would be a useful stop-gap. It'd just be frustrating to have to run it whenever an import changed.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: