Skip to content

[Feature] Ability to typeof imports #42482

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

Closed
5 tasks done
rraziel opened this issue Jan 25, 2021 · 3 comments
Closed
5 tasks done

[Feature] Ability to typeof imports #42482

rraziel opened this issue Jan 25, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@rraziel
Copy link

rraziel commented Jan 25, 2021

Suggestion

Expand the (very nice) ability to infer the type of an import() call expression so that such imports can be wrapped.

πŸ” Search Terms

typeof import

βœ… 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

Make it possible to expose the type of what a module exports:

function loadModule<T extends string>(modulePath: T): Promise<typeof import(T)>;

πŸ“ƒ Motivating Example

Library code that allows plugins, lazy-loading, etc. needs the user code to perform the import call itself in order to get typing information.

const plugin = await loadPlugin('@project/x/y', 'exportedFunction');

Without the feature, anything can be used instead of exportedFunction, but with the feature it would be possible to restrict the name and type the result:

function loadPlugin<
  ModuleName extends string,
  ExportName extends keyof typeof import(ModuleName)
>(
  moduleName: ModuleName,
  exportName: ExportName
): Promise<Plugin<typeof import(ModuleName)[ExportName]>>;

That's a fairly simple example, but it becomes more interesting when passing functions that would receive those exported objects as their parameters, etc.

πŸ’» Use Cases

Usage:

  • plugin mechanisms
  • lazy-loading features
  • build-time DSLs

Short-comings with the current approach:

  • nothing but import() can be used while keeping type information

Short-comings with the proposed approach:

  • tooling may sometimes need to be adapted to "detect" such new import constructs

Workarounds:

  • not many are available apart from importing the type/re-declaring separately
@DanielRosenwasser
Copy link
Member

Seems like a duplicate of #26139 and a duplicate of #41550.

@rraziel
Copy link
Author

rraziel commented Jan 25, 2021

Hm true, and a little bit #31090 as well
I suppose it's not worth keeping this one opened then, and instead I can go thumb-up the other ones or something πŸ˜…
edit: and #42046 aka I need to get better at searching for existing issues

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 29, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants