1

Given files

Path\Example.ts:

export module Example {
    ... Stuff ...
}

and Test.ts:

import { Example }    from "Path/Example";

let exampleMock = getExampleMock(); // getExampleMock returns an any that matches the type structure of Example

let e = exampleMock as Example; // Errors: Cannot find name 'Example'
let local = Example; // local is typed as Example;
local = exampleMock ; // since exampleMock is an any, compiler allows this call.

... use local with full typing of Example module 

Is there any way to type a variable to a module, without setting it to the real module first?

4
  • semi-related: I would really suggest moving away from "internal modules" if possible. According to your code you already use real modules, so I think it should be possible for you... Commented Feb 22, 2017 at 21:23
  • @st_huck, you mean how my Test.ts code isn't wrapped in a module? My Example.ts is exporting the module, so that would be an external module correct? Commented Feb 22, 2017 at 21:25
  • I meant I didn't understand why you use 'module' keyword in your Example.ts. Maybe it's intentional, just wanted to comment if it's not. Indeed, your example.ts in considered an external module, but what it exports is an internal module. You have no reason to use an internal module, because all your local variables in example.ts will still be private to the file, since it's an external module. maybe I'm the one missing something here? why not simply export const Example = {...}? Commented Feb 22, 2017 at 21:39
  • @st_huck I'm using Require JS to load my files, so I have to have everything that i want to import, defined in an module. My Test.ts in practice would be in a module as well, but it wasn't important to what i was doing. Does that answer the question? Maybe I need to brush up on internal modules... I just assumed that was dropping the export keyword from module. Commented Feb 22, 2017 at 21:44

1 Answer 1

3

Try

let exampleMock = getExampleMock() as typeof Example;
Sign up to request clarification or add additional context in comments.

Comments

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.