1

I have 2 modules spread across 2 files as such:

--App.Tools.Utils.ts

export interface ILogger {
    log: (msg: string) => void;
}

module App.Tools.Utils {     


    export class Logger implements ILogger { ... }

    export function someFunction(){ ... }
}

then in the second file I import the module:

--App.Routers.ts

 /// <reference path="App.Tools.Utils.ts" />
 module App.Routers { 

    import Utils = App.Tools.Utils;  //ERROR: A module cannot be aliased to a non-module type        

}

I eventually figured out the solution was to move the ILogger interface INSIDE the App.Tools.Utils module to resolve the error. At first it made some sense, I figured the compiler wouldn't let me import the module because the Logger class implemented an interface that was not included in the module. To test, I moved the ILogger interface inside the module (error resolved) but then added an arbitrary interface right outside the module (one that is not used inside the module or anywhere) and the error returns..

export interface INeverUsed { } //generates same error

module App.Tools.Utils {     

    export interface ILogger {
       log: (msg: string) => void;
    }

    export class Logger implements ILogger { ... }

    export function someFunction(){ ... }
}

Looking at the generated JS, adding the interface outside the module generates a define(["require", "exports"] wrapper and that results in the error when trying to import the App.Tools.Utils module in the other file. Removing the interface removes the define wrapper and the resolves the error.

Is it behavior expected? It makes no sense to me why a module is suddenly "closed" when I define an interface in the same file but outside the module, especially if that interface is not even used inside the module.

1 Answer 1

2

Because you are using the export keyword on the interface, the compiler is treating the file as a module.

If you remove the export keyword on the interface, it should work:

interface ILogger {
    log: (msg: string) => void;
}

module App.Tools.Utils {     
    export class Logger implements ILogger { 
        log(msg: string) { }
    }

    export function someFunction(){  }
}

And

///<reference path="game.ts" />

module App.Routers { 
    import Utils = App.Tools.Utils; // works      
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the reply. I'll mark as answer as soon as I test an assumption I made that I'm unable to test until later. The assumption is that if I leave the 'export' on the interface and add an export to the App.Tools.Utils module, the error should subside. My reasoning is that if the file is treated as a global module then 'export module App.Tools.Utils' should still be visible to the other file.
I tried that too - but it didn't seem to fix it. Let me know if I'm wrong on that point.
You are right, that does not resolve the error. Also, the 'export' on the module invalidates the ILogger inside the module unless the interface is also also exported.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.