-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Proposal: Enable declaration files for non-js-extensioned files #50133
Comments
I think it's great to support definition files for other file types. A solution that replaces the file extension like On top of that, the proposed solution to add another extension will create many more extensions to look for, (such as, My perhaps slightly radical proposal is to use a completely new dedicated extension like
|
I don't think we'd want to change our existing handling of ts/tsx/mts/cts/js/jsx/mjs/cjs, so they'd be an exception to any open-ended thing we do, regardless of the exact scheme. Otherwise, I'm not wholly opposed to a new extension like |
For Svelte we use the current system to create type declaration files next to their Svelte implementation in the case of npm library packaging:
This works great for us. If I understand correctly, the new proposal would be to have a |
|
Technically we now have working go-to-definition for non-JS files. I assume the |
@DanielRosenwasser , you mean for the case that both a global |
@DanielRosenwasser if a @idoros A |
This is exactly our case with Stylable. From our source |
I don't want to get too off topic, but since the UX discussion is relevant - have you considered using inline source maps in the generated |
We did consider inlining them, but as far as we could tell back then (and now, when re-testing) it doesn't actually seem to work in VSCode. Trying to go-to-definition brings me to the .d.ts file and not the source one. I can open an issue for this if you feel like it should be working. |
Closed by #51435 |
This would be really great for better support of MP3 files by TS. Current use case is adding a |
Suggestion
Ref #49970 and confusion therein.
⭐ Suggestion
Today, TS will only load declaration files for
.js
,.cjs
, and.mjs
files (and TS+jsx equivalents). There is no actual mechanism for specifying type definitions for a relative import for files with another extension - nonrelative imports can be defined using an ambient module declaration (see the widely useddeclare module "*.css";
), or viaimport
orexport
map entry, while relative imports have no mechanism available.In traditional cjs resolution, this wasn't obviously a problem, since if you wrote
require("./style.css")
, we would fail to load the.css
file (it's not a javascript or typescript extension so we wouldn't even try), and fall back to cjs directory and extension resolution - meaning we'd eventually look for"./style.css.js"
and accompanying"./style.css.d.ts"
, which was close enough for most people's purposes. Now, we have a prominent runtime that does not have extension searching (esm in node/some bundlers/browsers), where there's no mechanism to provide types for the specifier, but in some conditions do support importing non-js extension files.📃 Motivating Example
💻 Use Cases
Mostly bundlers and potentially browsers for css/html imports, but also
node
for the potential to type relative imports of.wasm
/.json
/.node
via declaration file, or other extensions as allowed via custom loader.📃 Proposal
Generally speaking, we want to keep a 1:1 mapping of runtime file extension to declaration file extension, this way we can always do a simple side-by-side lookup for the declarations for a file imported, a relatively simple mapping of output declaration path back to input filepath, and also capture any important format information implied by the original extension in the declaration filename (eg, if
.wasm
imports end up being importable as uninstantiated modules in the future, and other modules aren't). As such, any mechanism needs to be fairly unambiguous.Given that, I'd have to propose that a
maps to a
which is a declaration file (we'd have to update our definition of declarations to be
.d.ts
,.d.cts
,.d.mts
, and.d.*.ts
).This does a few things:
ext
to have a unique TS equivalentfilename.d.ts
, it doesn't also ambiguously map to afilename.js
orfilename.otherext
filename.ext.d.ts
, it doesn't already map to afilename.ext.js
filename.d.ext
, it retains a well-known.ts
final extension, for relatively painless tooling and editor supportThere are some considerations:
filename.d.ext.ts
source file today - it would be a TS source file (that emits afilename.d.ext.js
and afilename.d.ext.d.ts
) and not a declaration file, making this a technically breaking change. Such a filename is so unwieldy as to be unlikely in my view though, so I don't think this break should be too bad. In some ways, this is an upside, since external tooling will already recognize and parse these files as TS without modification, even if they don't see them as declarations yet.filename.d.js.ts
, which behaves identically to afilename.d.ts
, and we'd have to prioritize one of the two during lookup. Tentatively, I'd say we should just forbid ts and js extension patterns like this, so.d.js.ts
,.d.mjs.ts
,.d.cjs.ts
,.d.jsx.ts
,.d.ts.ts
,.d.tsx.ts
,.d.mts.ts
, and.d.cts.ts
shouldn't be looked up - the existing short forms take their place.mts
andcts
extensions; unfortunately this introduces some ambiguity, as all offilename.d.ext.ts
,filename.d.ext.mts
, andfilename.d.ext.cts
would map to the same originalfilename.ext
, rather than only one canonical one (and looking up the input filepath from an output declaration file is critical for scenarios like project references, which is why unambiguousness is so helpful here). We could just assume these declaration files are formatless, like ambient modules, and provide the same interface to both cjs and esm callers. That might be good enough to get by. Failing that, the only thing I can think of that preserves filename uniqueness would be some kind of in-declaration-file pragma for asserting the format of the containing file, or a compiler option specifying a global mapping of extension to format (though the later doesn't hold up well with redistributable libraries). But all that may be unnecessary, since, with the exception of potentially.json
, these should largely be authored by hand, so protection from format misuse is maybe less important than with JS files writ large.allowNonJsImports
, in the same vein asresolveJsonModule
).resolveJsonModule
is set, since it would canonicalizefilename.d.json.ts
as the declaration path for the json (and thus canonicalize loading such a declaration file at higher priority than the original json document in a wildcard include pattern).Thoughts?
The text was updated successfully, but these errors were encountered: