Matthias
Matthias

Reputation: 547

TypeScript: make class visible inside module only

Have a look at the following code:

module MyModule {
    class MyPrivateClass {
        ...
    }

    export class MyPublicClass {
        private var: MyPrivateClass; // MyPrivateClass is unknown
    }
}

I want MyPrivateClass to be only visible inside MyModule, specifically for internal use in MyPublicClass. Outside MyModule, only MyPublicClass should be visible. I figured that the above layout should do but VS complains that MyPrivateClass isn't visible inside MyPublicClass. Adding export before the definition of MyPrivateClass makes it visible to MyPublicClass but then it's also visible from the outside.

Is there a way to make it visible to MyPublicClass only?

Thank you.

Upvotes: 12

Views: 12843

Answers (4)

lhk
lhk

Reputation: 30256

You said in one of your comments:

It's strange: If I paste the code as above (yours or mine), it's fine. But as soon as I change the name of the module to its real name, the error I described appears

This sounds very similar to a problem I've experienced. It was caused because I had been using various reference paths to import the modules. As a result inside a module members couldn't access each other.

I'm sorry but I can't remember any more details and I haven't been able to reproduce your (or my) error. This is probably useless but I thought I would share my experience nevertheless: Mixing reference paths and modules seems to cause very strange errors.

Moreover sometimes VisualStudio behaves rather weird. I'm currently working on a typescript project together with a friend. The code is stored in a github repo. We both pulled the same version. I worked fine for me and was sprinkled with error messages for him. Same OS, same version of Typescript, same version of VisualStudio, ... Interestingly the error was related to modules, too. A module that was imported seemed to be "empty". All the code that tried to use the content of this module was marked red. He restarted VisualStudio and all of a sudden, the code was accepted as valid. We didn't change anything ! It compiled without problems, too.

Upvotes: 0

John Papa
John Papa

Reputation: 22338

If you want it to be private in the emmitted JavaScript you can do this by moving the private clas instance to inside the module, but not in side the exported class.

module MyModule {
    class MyPrivateClass {
        prop1: number = 1;
    }

    var foo: MyPrivateClass = new MyPrivateClass();

    export class MyPublicClass {
        someMethod(){
            var bar = foo.prop1;
        }
    }
}

var x = new MyModule.MyPublicClass();

Upvotes: 2

Fenton
Fenton

Reputation: 251242

Here is a working example that shows the private class, the public class, using the private class from the public class and attempting to use the private class, which generates an error.

If you still get an error, are you able to disclose the name you are trying to use for the module that causes the problem?

module MyModule {
    class MyPrivateClass {
        doSomething() {
            return 1;
        }
    }

    export class MyPublicClass {
        private x = new MyPrivateClass();

        someFunction() {
            return this.x.doSomething();
        }
    }
}

var examplePublic = new MyModule.MyPublicClass();
var result = examplePublic.someFunction(); // 1

var examplePrivate = new MyModule.MyPrivateClass(); // error

Upvotes: 7

Matthew Abbott
Matthew Abbott

Reputation: 61617

Hmm, I don't see any issues with it, but don't forget to initialise the field value, otherwise the compiler won't generate the field:

module MyModule {
    class MyPrivateClass {

    }

    export class MyPublicClass {
        private instance: MyPrivateClass; // MyPrivateClass is unknown
        constructor() {
           this.instance = new MyPrivateClass();
        }
    }
}

Upvotes: 0

Related Questions