TypeScript:使类仅在模块内可见

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12781767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:13:46  来源:igfitidea点击:

TypeScript: make class visible inside module only

typescript

提问by vexator

Have a look at the following code:

看看下面的代码:

module MyModule {
    class MyPrivateClass {
        ...
    }

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

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

我希望MyPrivateClass只在MyModule内可见,特别是在MyPublicClass内部使用。在MyModule之外,只有MyPublicClass应该是可见的。我认为上面的布局应该可以,但 VS 抱怨MyPrivateClassMyPublicClass 中不可见。在MyPrivateClass的定义之前添加 export使其对MyPublicClass可见,但它也从外部可见。

Is there a way to make it visible to MyPublicClassonly?

有没有办法让它只对MyPublicClass可见?

Thank you.

谢谢你。

回答by Fenton

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

回答by John Papa

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.

如果您希望它在发出的 JavaScript 中是私有的,您可以通过将私有 clas 实例移动到模块内部而不是导出类的内部来实现。

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();

回答by lhk

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.

此外,有时 VisualStudio 的行为很奇怪。我目前正在和一个朋友一起做一个打字稿项目。代码存储在 github 存储库中。我们都拉了同一个版本。我对我来说有效得很好,并为他散布了错误消息。相同的操作系统,相同版本的 Typescript,相同版本的 VisualStudio,......有趣的是,错误也与模块有关。导入的模块似乎是“空的”。所有试图使用该模块内容的代码都被标记为红色。他重新启动了 VisualStudio,突然间,代码被接受为有效。我们没有改变任何东西!它编译也没有问题。

回答by Matthew Abbott

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();
        }
    }
}