Daryl
Daryl

Reputation: 18895

How to type a variable to a module in Typescript?

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?

Upvotes: 1

Views: 415

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164137

Try

let exampleMock = getExampleMock() as typeof Example;

Upvotes: 3

Related Questions