2015-11-26 90 views
1

我正在使用TypeScript 1.6.2和atom-typingcript。尝试使用在不同的文件命名空间:如何在文件之间使用命名空间?

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

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

我尝试使用引用,但仍然没有工作,也有一个有效的tsconfig.json,它的设置来解决的定义和文件中包含的:

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

enter image description here

回答

2

因为你`进口*为_从 'lodash',这使得该文件本身就是一个模块https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

如果您使用的模块...不要使用名称空间作为它只是一个不必要的间接点。每个文件的变量(包括名称空间)都不同于其他文件。

+0

谢谢Basarat!但是我不能利用名称空间来不需要相对路径吗?像''从'../../../../ Service.ts''导入{something}我以为我可以使用命名空间来让编译器解析文件的位置...... – Hadrian

+0

不幸的是,目前尚不支持。它的一个负担留在其他*构建工具像webpack *或*运行时像nodejs * – basarat

+0

所以最好的选择是等待1.8和[在模块分辨率中使用路径映射](https://github.com/Microsoft/TypeScript/问题/ 5039) – Hadrian