Skip to content
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

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

Open
llgcode opened this issue Nov 3, 2020 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@llgcode
Copy link

llgcode commented Nov 3, 2020

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

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Nov 12, 2020
@jdom
Copy link
Member

jdom commented Oct 28, 2022

I'm also interested in this exact same thing. Is it possible to do?

@RyanCavanaugh
Copy link
Member

This currently isn't architecturally possible: There's one pass that collects all the files to be parsed, then type checking happens on a subsequent pass. We'd have to make the file-reading pass re-entrant to support this (which, given two comments in two years, isn't an architectural change we'd have much appetite for).

@jdom
Copy link
Member

jdom commented Oct 28, 2022

sounds good, we do have a workaround, where we do this mapping from a particular string literal to the typeof import, and that works ok. I was just hoping to clean up this manual mapping of hundreds of lines (1 line per module we have).
Thanks for the reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants