Skip to content

Support inferring module specifier to allow custom import function #26139

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
4 tasks done
mohsen1 opened this issue Aug 2, 2018 · 3 comments
Closed
4 tasks done

Support inferring module specifier to allow custom import function #26139

mohsen1 opened this issue Aug 2, 2018 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Aug 2, 2018

Search Terms

Modules, import(), Module Specifier, Module Namespace

Suggestion

Support inferring module specifier passed to type import function (import() in type name space) to allow custom import function.

Use Cases

if I have a function that wraps require, it's impossible to declare types for it:

function myRequire(moduleSpecifier) {
  return require(moduleSpecifer);
}

Ideally I would like to declare types for a function like that as following:

declare function myRequire(moduleSpecifier: string): import(moduleSpecifier);

Use Cases

  • Custom require functions are very common, specially for testing where they do mocking
  • Jest mock functions can take advantage of this

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. new expression-level syntax)
@AlCalzone
Copy link
Contributor

It's not entirely impossible, but doesn't look nice either (and you have to duplicate the import specifier):

function load<T>(stuff: string): T {
	return require(stuff) as T;
}

const foo = load<typeof import("./index")>("./index");

@mohsen1
Copy link
Contributor Author

mohsen1 commented Aug 2, 2018

@AlCalzone That's what we're doing currently but repeating the module specifier and all that extra syntax is why I opened this issue.

/cc @DanielRosenwasser

@ghost
Copy link

ghost commented Aug 2, 2018

This issue would be hard to fix the way the compiler is designed currently, since it means we would have to load in new files while type-checking. Currently we can get the full set of imports just by parsing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

3 participants