Skip to content

Support inferring module specifier to allow custom import function  #26139

Closed
@mohsen1

Description

@mohsen1

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions