Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you create nested classes in TypeScript?

Is there a way to nest classes in TypeScript. E.g. I'd like to use them like:

var foo = new Foo(); var bar = new Foo.Bar(); 
like image 399
basarat Avatar asked Sep 10 '15 05:09

basarat


People also ask

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.

Can you have classes in TypeScript?

TypeScript supports object-oriented programming features like classes, interfaces, etc. A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object. Typescript gives built in support for this concept called class.

Can you have nested classes in JavaScript?

Let us try to understand, what is class. A class in JavaScript is a type of function, which can be initialized through both function keyword as well as through class keyword. In this article, we will cover the inner class in javascript through the use of a function keyword.

Can classes be nested?

Terminology: Nested classes are divided into two categories: non-static and static. Non-static nested classes are called inner classes. Nested classes that are declared static are called static nested classes. A nested class is a member of its enclosing class.


1 Answers

In modern TypeScript we have class expressions which you can use to create a nested class. For example you can do the following :

class Foo {     static Bar = class {              } }  // works! var foo = new Foo(); var bar = new Foo.Bar(); 
like image 154
basarat Avatar answered Oct 07 '22 08:10

basarat