Currently, I am dealing with a package that contains numerous generated modules all exporting the same type, such as an icon library.
I am trying to avoid the tedious task of creating individual .d.ts
files for each module since they would essentially be duplicates.
My idea is to have a single index.d.ts
file located at the root of the package structured like this:
// pkg-with-many-assets/index.d.ts
declare module 'pkg-with-many-assets/*' {
const svgPath: string;
export default svgPath;
}
However, I am encountering issues where consuming apps are not recognizing this setup. Surprisingly, it does work when I place the declaration file within the consuming app.
Could there be a way to create a package with an ambient module declaration?
Update:
It turns out that it is feasible because the @types/simple-icons package has implemented this approach successfully. However, when I attempt to copy and paste the @types/simple-icons/index.d.ts
file into my own simple-icons/index.d.ts
directory in the node_modules
folder, it stops functioning.
The problem might lie in TypeScript failing to read the index.d.ts
file situated at the root of the simple-icons
package, despite having the
"types": "index.d.ts"
specification in the package.json
file.
To solve this issue, I was able to make it work by explicitly specifying the package name in the tsconfig.json
under compilerOptions.types
. Nevertheless, I prefer to have this detection process automated without manual intervention.