在具有命名空间的文件中导入库导致错误

时间:2017-06-08 04:25:29

标签: javascript node.js typescript ecmascript-6

当我尝试在一个带有命名空间typescript的外部文件中导入库时无法编译它们,如何解决这个问题?

a.ts

namespace NS {    
    export class A {
        constructor(){

        }
        test(): string {
            return (new B()).callFromA();
        }        
        callFromB(): string {
            return "Call From B";
        }
    }
}

b.ts

namespace NS {    
    export class B {
        constructor(){

        }
        test(): string {
            return (new A()).callFromB();
        }
        callFromA(): string {
            return "Call From A";
        }
    }
}

main.ts

/// <reference path="./libs/a.ts" />
/// <reference path="./libs/b.ts" />
console.log(new NS.A().test());
console.log(new NS.B().test());

输出控制台

tsc --outFile sample.js main.ts libs/a.ts libs/b.ts && node sample.js Call From A Call From B

现在,如果我在一个文件中导入一个库,

b.ts

import fs = require(fs);
namespace NS {    
    export class B {
        constructor(){

        }
        test(): string {
            return (new A()).callFromB();
        }
        callFromA(): string {
            return "Call From A";
        }
    }
}

输出控制台

tsc --outFile sample.js main.ts libs/a.ts libs/b.ts && node sample.js libs/a.ts(7,25): error TS2304: Cannot find name 'B'. libs/b.ts(1,1): error TS6131: Cannot compile modules using option 'outFile' unless the '--module' flag is 'amd' or 'system'. libs/b.ts(9,25): error TS2304: Cannot find name 'A'. main.ts(4,20): error TS2339: Property 'B' does not exist on type 'typeof NS'.

0 个答案:

没有答案