Improve declare module wildcards so that they can match parts of a path more like a glob
See original GitHub issueSearch Terms
global module match glob path
Suggestion
Improve the declare module wildcards so that they can be used multiple times in a path and they can match multiple parts of it.
Use Cases
In the project I am currently working on we make a distinction between images and icons:
- icons are usually small in size and used multiple times so we store them as SVG and put them in a SVG sprite with the help of external-svg-sprite-loader.
- images are usually bigger in size and used only once so we store them as well as SVGs but do not put them in a sprite.
Since SVG imports are not recognized by TypeScript I want to create a global module declaration that targets all icon imports and another that targets all image imports. However, based on the feedback I got in this StackOverflow issue, it doesn’t seem to be possible to do this since the support of wildcards in module paths seems to be very limited.
Examples
This is an example of what I would like to be able to do:
declare module '@my/**/icons/**/*.svg' {
type IconType = { viewBox: string, symbol: string };
const icon: IconType;
export = icon;
}
declare module '@my/**/images/**/*.svg' {
const url: string;
export = url;
}
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
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:27
- Comments:6
Top Results From Across the Web
How to create a TypeScript module that matches complex ...
Try to put declare the modules by file suffix, one declaration per file type. declare module '@my/module/src/images/*.svg' { type IconType ...
Read more >Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >Understanding the glob pattern in Node.js - LogRocket Blog
The glob pattern is most commonly used to specify filenames, called wildcard characters, and strings, called wildcard matching.
Read more >How to Write a PowerShell Module Manifest - Microsoft Learn
For example, you can describe the author, specify files in the module (such as nested modules), run scripts to customize the user's environment, ......
Read more >latest - Environment Modules
Typically modulefiles instruct the module command to alter or set shell environment variables such as PATH , MANPATH , etc. Modulefiles may be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1
This is a problem that loaders for most of JS bundlers like Webpack or Vite have. I wrote a Vite plugin that loads SVG assets as React components and have the following module declaration:
What I would really want is simply…
…to allow the addition of asset specific parameters as part of the query, like this:
I’m also using
vite-imagetools
and came across this same problem. The best I could come up with is appending&imagetools
to the end of my query for each image. Not ideal whatsoever since the query parameter serves no purpose, but it does work:Still, it would save some unnecessary code to have the ability to write complex wildcards.