Skip to content

Type detection for dynamic imports with template strings #51726

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

Open
5 tasks done
danielrentz opened this issue Dec 2, 2022 · 3 comments
Open
5 tasks done

Type detection for dynamic imports with template strings #51726

danielrentz opened this issue Dec 2, 2022 · 3 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@danielrentz
Copy link

Suggestion

πŸ” Search Terms

dynamic imports template string typed

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Dynamic imports using a simple template string could be matched against wildcard module declarations.

πŸ“ƒ Motivating Example

I am using the Rollup plugin dynamic-import-vars to handle import expressions with variable module names, for example to load image files as encoded strings:

const module = await import(`./images/${name}.png`);

There is a wildcard declaration for these files in the project:

declare module "*.png" {
    const data: string;
    export default data;
}

It would be great if the compiler tries to match such wildcard module declarations against template strings, maybe in a strict limited way such as:

  • only module declarations with a single leading or trailing asterisk
  • only against import expressions with a single template string containing leading/trailing matching characters (or string concatenation with matching string literals at beginning/end)

πŸ’» Use Cases

See example above. The workaround in this situation is to explicitly write a type assertion:

const module = await import(`./images/${name}.png`) as typeof import("*.png");
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Dec 2, 2022
@KevinMind
Copy link

Another use case here would be to infer return type of functions based on a dynamic key specifying the file path to import from. Wicked for file based routing systems like remix.run

export function useRouteLoaderData(routeID: RouteID): typeof import(`./${typeof routeID}`).loader {
    return _useRouteLoaderData(routeID);
}

@suguanYang
Copy link

suguanYang commented Feb 21, 2025

I thought this one is similar to #32401.

@danielrentz
Copy link
Author

@suguanYang Right that issue sounds similar. While it is about using the type of a constant defined elsewhere in import expressions, this issue here is about the special case of wildcard module definitions and dynamic template strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants