0

I am dynamically loading modules I have created like so:

myModule.ts

export class SomeClass {
 ...
}
export type SomeType = {...}

In another file, I do:

import * as MyModuleType from "./myModule"
if(...) {
  let loadedModule: typeof MyModuleType = require("./myModule");
  ...
}

The type of MyModuleType has both SomeClass and SomeType (as expected). Whereas the type of loadedModule has only SomeClass (incomplete type!). How can I solve this issue?

Version of typescript: 2.3.2

editor: atom, using atom-typescript v10.1.15

Thank you.

1 Answer 1

1

Types in TS descripe values. so typeof MyModuleType describes the shape of the module value at run time. The module value only has one property, i.e. SomeClass. And it does not have any property SomeType.

If you want to get the type jsut reference the import:

var x: MyModuleType.SomeType;

The TS compiler still knows that your import is only used as a type and will be elided.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply, what you said works if the import statements and the dynamic require statement are in one file. What if I want to pass loadedModule to another file, and I want to do so without having an import * as .... statement in that file. How will I then get "SomeType" access via loadedModule?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.