Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript private class inside class or something similar

I'd like to declare a private class, only to be used inside the file it was defined, is this possible? For example declared inside another class:

export class MyParentClass {     class MyChildClass {     } } 

or inside the same file:

export class MyPublicClass {    //Usage of MyPrivateClass  }  class MyPrivateClass { } 
like image 541
MR.ABC Avatar asked Jun 18 '13 07:06

MR.ABC


People also ask

How do you use class inside class in TypeScript?

To create nested classes in TypeScript, we can create a static class property in our class. class Foo { static Bar = class {}; } const foo = new Foo(); const bar = new Foo. Bar(); to create the Foo class that has the static Bar property which is set to a class expression.

How do you access private members of a class in TypeScript?

In TypeScript there are two ways to do this. The first option is to cast the object to any . The problem with this option is that you loose type safety and intellisense autocompletion. The second option is the intentional escape hatch.

Does TypeScript support nested classes?

Is it possible to define nested/inner class in typescript? Yes, it is. Go to chapter 3 or 4 if you want to know only nested/inner class.

Does TypeScript have private?

TypeScript Private Properties Using TypeScript, we can add private functionality into our classes. What are private properties or methods? A private property of method can only be accessed or called from the class instance itself.


1 Answers

module MyModule {     export class MyPublicClass {         private myPrivateClass: PrivateClass;         constructor() {             this.myPrivateClass = new PrivateClass;         }         public test() {             this.myPrivateClass.test();         }     }      class PrivateClass {         public test() {             console.log('it works');         }     } } 
like image 194
Vertigo Avatar answered Oct 06 '22 10:10

Vertigo