6

I want to split class definitions in a same module into multiple files. So I did like this and it worked.

a.ts:
module MyModule{
    class ClassA{
    }
}

b.ts:
module My Module{
    class ClassB{
    }
}

Then I tried to use ClassA in ClassB and did that:

b.ts:
///<reference path="a.ts"/>
module MyModule{
    class ClassB{
        private a:ClassA;
    }
}

But it didn't work; "ClassA" needed to be "MyModule.ClassA" in b.ts though they're in the same module.

I prefer a simpler way like above. Do you have any ideas?

1 Answer 1

2

You can solve your problem by making the class public:

module MyModule{
    export class ClassA{
    }
}

I'm not sure why you need to do this as really they are part of the same module - but this seems to be the case.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much.It worked. The reason I do this is that the source would be very large in one file. Essentially ClassA doesn't need to be public, so this is a bit uncomfortable, but I think it's better than before.
Splitting a module across multiple files is good practice, so no arguments from me there.
I don't think this is the right solution because it changes the semantics of ClassA. I realize that this is how TypeScript implements this but still.
@buzzy To keep ClassA private, you simply put it in the same module declaration. The only thing you can't do is both. It is also worth bearing in mind TypeScript doesn't support internal or protected access modifiers, so you will find yourself using public more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.