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 variable as typeof external module without import #4687

Closed
Roam-Cooper opened this issue Sep 8, 2015 · 3 comments
Closed

Declare variable as typeof external module without import #4687

Roam-Cooper opened this issue Sep 8, 2015 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@Roam-Cooper
Copy link

Hey there,

Is it possible to write an external (commonjs) module "A", then in another module "B"'s code, declare a variable as being typeof module "A" without using import which ends up require-ing module "A".

The reason I want to do this is that I will be passing a reference to module "A" to the constructor of a class in another module "B", because I want module "A" to be a singleton. But I want to type the argument in the constructor to that of module "A", as if module "A" had been imported using import A = require("A");

The reason I want a singleton module is that this module might contain instances of classes that manage database connections, etc, and I don't really want them re-initialising each time I have to import; I want to be able to pass them around rather than rely on potential "node-module cache" magic.

Is there a way to do this? I've thought of other ways I can achieve want I want but this feels like the simplest, if it's possible.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 8, 2015

Module imports are elided if they are not used in a value position. so if your import of module A is only used in a type position the require call will not be written out.

for instance:

// B.ts
import s = require("./A");
var x: typeof s;

emits

var x;

does this answer your question?

@mhegazy mhegazy added Question An issue which isn't directly actionable in code Help Wanted You can do this and removed Help Wanted You can do this labels Sep 8, 2015
@Roam-Cooper
Copy link
Author

Oh, that's great! It definitely answers my question. I couldn't find this in the documentation, so thanks a tonne for your help. :)

@BaamAadmi
Copy link

Hi Folks,
I have a question which is on the same lines. I tried above solution but it did now work for me.
I am importing classes from a webpack module parent import { A, B } from "parent";
I simply want to call constructor for these classes. I tried const a = A( { data: 1} ); This does not give any TS error on compile. But it breaks on runtime javascript saying , Type error A is not a constructor.

Do I need to create custom types and add it in devDependancies ? Or is there a specific way to export my "parent" module like export default.

Any help is appreciated.

Cheers,
Sudeep

@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants