Search code examples
javascripttypescriptvisual-studio-2015

TypeScript namespace merging across files


I'm having some issues with getting a namespace across files to resolve and compile properly. According to documentation and this SO answer, the following should produce no issue:

App.Core.ts

namespace App.Core {
    export function createElem(tagName: string): JQuery {
        return $(document.createElement(tagName));
    }
}

App.Core.SubModule.ts

/// <reference path="App.Core.ts" />
namespace App.Core.SubModule {
    export function Test(): JQuery {
        return App.Core.createElem("div");
}

However, Visual Studio is giving me an error on the function call in App.Core.SubModule.Test stating Property 'createElem' does not exist on type 'typeof Core' My understanding is that if a namespace is across multiple files TS compiler will automatically resolve those namespaces. It looks like the corresponding JavaScript is coming out correct however the lack of intellisense (and the red squiggle error line) is exceptionally frustrating and making me second guess what I'm writing.

Is this an issue with my file setup, with TS compiler, or with Visual Studio 2015's apparently broken TypeScript intellisense functionalities right now?


Solution

  • Visual Studio is giving me an error on the function call in App.Core.SubModule.Test stating Property 'createElem' does not exist on type 'typeof Core'

    Suspect you have a root level import or export in either of the files. That makes the file a module and disconnects it from the global namespace.

    Quick Fix

    Remove that import / export but be warned that it can lead to more problems https://basarat.gitbook.io/typescript/main-1/outfile

    Proper Fix

    Don't use namespace. Just use modules : https://basarat.gitbook.io/typescript/project/modules/external-modules