Search code examples
typescripttypescript1.6

How to use namespaces between files?


I'm using TypeScript 1.6.2 and atom-typescript. Trying to use a namespace on separate files:

// Main.ts
import * as _ from 'lodash'
namespace Test
{
  export var value = true
}

// Another.ts
namespace Another
{
  function Testing()
  {
    return Test.value == true
  }
}

I tried using references, but still didn't work, also have a valid tsconfig.json and it's setup to resolve the inclusion of definitions and files:

{
"compilerOptions": {
    "noEmit": true
},
"compileOnSave": false,
"filesGlob": [
    "./**/*.ts",
    "./Scripts/typings/**/*.d.ts",
    "!./node_modules/**/*.ts"
],
"files": [
    "./Scripts/typings/tsd.d.ts"
]
}

enter image description here


Solution

  • Because you have `import * as _ from 'lodash', that makes the file a module on its own. https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

    If you are using modules ... don't use namespaces as its just an needless point of indirection. Each file's variables (including namespaces) are distinct from other files.