Marc HPunkt
Marc HPunkt

Reputation: 439

Typescript export module explicitly creates new namespace?

I just noticed this behaviour and can't fully wrap my head around it.

So I have two typescript files File1.ts File2.ts

File1.ts :

module myNamespace {
    export class myClass1 {
         someMethod(myClass2) // use this class
    }
}

File2.ts:

module myNamespace {
    export class myClass2 {
    }
}

This works and makes sense since both classes end up in the same namespace after the compilation.

However, when I change File2.ts into this

export module myNamespace {
    export class myClass2 {

    }
}

When I add the export keyword in front of the module keyword the typescript compiler complains that it can no longer find myClass2 in File1.ts

Why is that? Is it because export explicitly sets up a new namespace regardless if it already exists? The created javascript is nearly identical for File2.ts except for

define(["require", "exports"], function(require, exports) { .. }

Does this define call crate a new closure I have to dig into?

Sorry if this is a bit convoluted.

Upvotes: 1

Views: 756

Answers (1)

Alex Dresko
Alex Dresko

Reputation: 5203

I think it's just a compiler requirement. On the playground I get this error with your code:

enter image description here

Everything works fine, however, if I change the code to this:

enter image description here

or this..

enter image description here

In both cases, the output code is the same.

Upvotes: 1

Related Questions