How can I add type declarations to a local non-JavaScript file?

251 Views Asked by At

I have a plugin for my bundler (in my case Vite), that loads files, that are neither JS nor TS (the plugins simply transforms the file to valid JavaScript on import).

I want to provide typescript declarations for those non-JavaScript files.

Things I have already tried / considered

Wildcard module declarations

As described here
I could go with a similar approach to what Vue does for example:

declare module '*.vue' {
   export default MyFancyType;
}

This does not work for me, because all files have different contents and should therefore have different types.

Using declare module with paths

I have also tried using declare module with a relative and absolute path of the file.
This won't work for two reasons:

  1. TS errors with Ambient module declaration cannot specify relative module name.
  2. Relative paths won't work anyways, because I could have imports from multiple TS files with different relative paths.
Adding a myfile.ext.d.ts file next to the imported file

This works in theory, but in my case that would mean A LOT of files. So this is undesirable.

0

There are 0 best solutions below