Import the library into a file with a namespace. Mistake

when I try to import a library into one of the external file with typescript namespace, can't compile them, how to do it?

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());

      

output console

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

now if i import the library into one of the files,

b.ts

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

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

      

output console

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'.

+3


source to share





All Articles