1

I've got a conundrum for you. I'm fairly new to Typescript, but I've enjoyed the experience thus far. Wanting to be a good developer and logically group my modules into namespaces I've come across a rare? issue. Here's the simplest example to recreate the problem.

Lets say you got a class... we'll call it ClassA, which references a class aptly named ClassB which resides in a child namespace.

namespace Foo {
    export class ClassA {
        public classB: Bar.ClassB;
        public constructor() {

        }
    }
}

and

namespace Foo.Bar {
    export class ClassB {
        public constructor() {

        }
    }
}

Typescript couldn't be happier with this. Well when I want to import things for ClassA, I'll put it inside the namespace like so:

namespace Foo {
    import SomeClass = SomewhereElse.SomeClass;
    export class ClassA {
        public classB: Bar.ClassB;
        public constructor() {

        }
    }
}

Again... we cool. But what if I wanted to import an ambient module?

namespace Foo {
    import * as SockJS from "sockjs-client"; // TS1147 GRUMBLE! CAN'T DO DAT HERE!
    export class ClassA {
        public classB: Bar.ClassB;
        public constructor() {

        }
    }
}

Ok, that's fine. I'll just move it outside the namespace like it's telling me to.

import * as SockJS from "sockjs-client";

namespace Foo {
    export class ClassA {
        public classB: Bar.ClassB; // TS2503:Now you're just a Bar that I used to know
        public constructor() {

        }
    }
}

Suddenly Typescript gets amnesia and doesn't know what Bar is. It doesn't recognize child namespaces. I've tried several ways to make it remember like nesting the namespaces or exporting them, but nothing will make it recognize this. What's the dilly yo? Any of you cool cats know what's going on or how I can resolve it and keep my child namespaces?

Zach
  • 3,157
  • 1
  • 19
  • 32
  • I think this post will give you answers you are looking for: http://stackoverflow.com/questions/30357634/how-do-i-use-namespaces-with-typescript-external-modules – Amid Mar 29 '16 at 19:31
  • 1
    And just in case - one more: https://www.stevefenton.co.uk/2015/05/stop-mixing-typescript-internal-and-external-modules/ – Amid Mar 29 '16 at 19:45
  • The second one was slightly more helpful then the first. The first post you provided didn't really answer the question. It was a long way of saying "Don't" and sweetened the deal with some pictures of candy... which I can't eat. The second one stated they are mutually exclusive and gave a little better reasoning - although neither gave a satisfying explanation as to why it's this way. Regardless, I have resigned myself to except this. Thanks @Amid. – Zach Mar 29 '16 at 20:20

1 Answers1

2

Suddenly Typescript gets amnesia and doesn't know what Bar is. It doesn't recognize child namespaces. I've tried several ways to make it remember like nesting the namespaces or exporting them, but nothing will make it recognize this. What's the dilly yo

Its because namespaces are global. And modules are modular. So as soon as you import a module into your file, your file becomes a module. https://basarat.gitbooks.io/typescript/content/docs/project/modules.html

Suggestion

Please use modules. There are plenty of reasons : https://github.com/TypeStrong/atom-typescript/blob/8d43dd1b930a6df0ce62454a1560acfb7eee24c9/docs/out.md

j.raymond
  • 1,845
  • 1
  • 15
  • 15
basarat
  • 261,912
  • 58
  • 460
  • 511