microsoft / microsoft/TypeScript

Declare type to be an import depending on a string literal argument: typeof import(name);

Open
#41,382 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Hello,
It's seems to be difficult to do but there's now a lot of possibilities with the typescript compiler that I wouldn't have thought possible before, so I prefer to ask.

Is there a chance to have this kind of definition:

declare function require(name: string): typeof import(name) | undefined;

I mean declaring the return type as the type coming from the import of module "name". So we could have the type returned by require:

var module = require("my module");

I guess if it's feasible, it will also be possible to add more typing on this kind of javascript code

define("myModule, ["deps1", "deps2", "deps3"], (deps1, deps2, deps3) => {
// deps1, deps2, deps3 have the right type !!
});

like this

define("myModule, ["deps1", "deps2", "deps3"], (deps1: typeof import("deps1"), deps2: typeof import("deps2"), deps3: : typeof import("deps3")) => {
// deps1, deps2, deps3 have the right type !!
});

Thanks for your help,

--
Laurent

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by investigating whether the TypeScript compiler can make typeof import(name) depend on a string literal and use that result for require and define calls. Review the issue's examples and determine the language and type-checking changes needed; done would mean the requested module types are inferred from literal arguments.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.