typescriptsystemjsjavascript-namespaces

How to correctly import the same namespace in two different files in Typescript


I'm having an issue in understanding how to make two files with the same namespace and import one into the other in Typescript.

This is the situation:

file1.ts

export namespace nspace {
    export class ClassA {

    }
}

file2.ts

import {nspace} from './file1';

export namespace nspace {
    export class ClassB extends nspace.ClassA {
        private z: nspace.ClassA;
    }
}

inside file2 i have these errors:

1) Individual declarations in merged declaration 'nspace' must be all exported or all local

2) Property 'ClassA' does not exist on type 'typeof nspace'

On top of that, ClassA is correctly found when used to declare the type of the z field (it even brings me to the correct file if i use "go to declaration" in my IDE)
I have tried searching the first error on the internet because i don't really understand what it means but none of the pages i found helped. I read the documentation about Declaration Merging in Typescript but i couldn't find a situation similar to mine
I don't know if this is of any help but i'm using SystemJS in my application


Solution

  • Apparently, giving another name when importing worked:

    file1

    export namespace nspace {
        export class ClassA {
    
        }
    }
    

    file2

    import * as a from './file1';
    
    export namespace nspace {
        export class ClassB extends a.nspace.ClassA {
            private  b: a.nspace.ClassA;
    
        }
    }