Skip to content
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

Improve declare module wildcards so that they can match parts of a path more like a glob #38638

Open
5 tasks
bensampaio opened this issue May 18, 2020 · 7 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@bensampaio
Copy link

Search 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.
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jun 8, 2020
@lucsky
Copy link

lucsky commented Sep 3, 2021

+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:

declare module '*.svg?component' {
    ...
}

What I would really want is simply...

declare module '*.svg?component*' {
    ...
}

...to allow the addition of asset specific parameters as part of the query, like this:

import Logo from 'graphics/logo.svg?component&memo&titleProp=true';

@ninofiliu
Copy link

@isaac-mcfadyen
Copy link

Any progress/update on this? Tools like vite-imagetools make heavy use of imports like *.jpeg?*.

@silasabbott
Copy link

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:

declare module "*&imagetools" {
    const out;
    export default out;
}
import Image from '$lib/images/profile.jpg?w=50&h=50&format=webp&imagetools'

Still, it would save some unnecessary code to have the ability to write complex wildcards.

@Nantris
Copy link

Nantris commented Sep 23, 2022

It would be great if we could do like ours-somemodule-platform and target it with ours-*-platform

@Qtax
Copy link

Qtax commented Dec 29, 2022

Another tool that needs this is image-minimizer-webpack-plugin.

Instead of declare module '*.jpg?*' ... I'm forced to use an ugly hack like declare module '*&imgmin' ... to avoid these errors:

TS2307: Cannot find module './image.jpg?width=200' or its corresponding type declarations.

@thesoftwarephilosopher
Copy link

I just ran into the issue that foo-bar.md doesn't match *.md but foobar.md does. There's no way to fix this as far as I can tell.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

9 participants