How to Create Nested Classes in TypeScript ?
Last Updated :
28 Apr, 2025
In TypeScript, you can create nested classes using different methods. We will discuss about three different approaches to creating nested classes in TypeScript.
These are the approaches:
By defining nested classes inside a class
In this approach, we will directly define the nested class inside the outer class.
Syntax:
class OuterClass{
static InnerClass = class{
}
}
Example: The below code example defines a nested class inside another class.
JavaScript
class OuterClass {
outerClassProperty: string;
constructor(outerClassProperty: string) {
this.outerClassProperty = outerClassProperty;
}
outerClassMethod() {
console.log
(`Outer Class property:
${this.outerClassProperty}`);
}
// Nested class
static InnerClass = class {
innerClassProperty: string;
constructor(innerClassProperty: string) {
this.innerClassProperty = innerClassProperty;
}
innerClassMethod() {
console.log
(`Inner Class property:
${this.innerClassProperty}`);
}
}
}
const outerClassInstance = new OuterClass("GeeksforGeeks");
outerClassInstance.outerClassMethod();
const innerClassInstance =
new OuterClass.InnerClass("A Computer Science Portal");
innerClassInstance.innerClassMethod();
Output:
Outer Class property: GeeksforGeeks
Inner Class property: A Computer Science Portal
By using the namespaces
The namespace can also be used to create a namespace of Outer class and the namespace of the inner class which can be later merged together to create nested classes.
Syntax:
namespace namespace_name{
export class OuterClass{}
}
namespace namespace_name.OuterClass{
export class InnerClass{}
}
Example: The below code example will help you to create the nested classes using the namespace approach.
JavaScript
namespace nestedClasses {
export class OuterClass {
outerClassProperty: string;
constructor(outerClassProperty: string) {
this.outerClassProperty = outerClassProperty;
}
outerClassMethod() {
console.log
(`Outer Class property:
${this.outerClassProperty}`);
}
}
}
namespace nestedClasses.OuterClass {
export class InnerClass {
innerClassProperty: string;
constructor(innerClassProperty: string) {
this.innerClassProperty = innerClassProperty;
}
innerClassMethod() {
console.log
(`Inner Class property:
${this.innerClassProperty}`);
}
}
}
const outerClassInstance =
new nestedClasses.OuterClass("GeeksforGeeks");
outerClassInstance.outerClassMethod();
const innerClassInstance =
new nestedClasses.OuterClass
.InnerClass("A Computer Science Portal");
innerClassInstance.innerClassMethod();
Output:
Outer Class property: GeeksforGeeks
Inner Class property: A Computer Science Portal
By using the modules
The modules can also be used to create nested classes in TypeScript in the same way as we used namespace.
Syntax:
namespace namespace_name{
export class OuterClass{}
}
namespace namespace_name.OuterClass{
export class InnerClass{}
}
Example: The below example uses the modules to define the nested classes in TypeScript.
JavaScript
module nestedClasses {
export class OuterClass {
outerClassProperty: string;
constructor(outerClassProperty: string) {
this.outerClassProperty = outerClassProperty;
}
outerClassMethod() {
console.log
(`Outer Class property:
${this.outerClassProperty}`);
}
}
}
module nestedClasses.OuterClass {
export class InnerClass {
innerClassProperty: string;
constructor(innerClassProperty: string) {
this.innerClassProperty = innerClassProperty;
}
innerClassMethod() {
console.log
(`Inner Class property:
${this.innerClassProperty}`);
}
}
}
const outerClassInstance =
new nestedClasses.OuterClass("GeeksforGeeks");
outerClassInstance.outerClassMethod();
const innerClassInstance =
new nestedClasses.OuterClass
.InnerClass("A Computer Science Portal");
innerClassInstance.innerClassMethod();
Output:
Outer Class property: GeeksforGeeks
Inner Class property: A Computer Science Portal
Similar Reads
How to Create an Object in TypeScript? TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data.Creating Objects in TypescriptNow, let
4 min read
How to use class syntax in Typescript ? Classes: The class keyword was introduced in ES2015. TypeScript fully supports 'class' keyword. classes are a template for creating objects. A Class is a user defined data-type which has data members and member functions. Data members are the data variables and member functions are the functions use
2 min read
How to implement class constants in TypeScript ? In this article, we will try to understand how to create several class constants (properties with constant values) in TypeScript with the help of certain code examples for better concept understanding as well as clarification. Let us first understand quickly how we may create a class in TypeScript w
3 min read
How to Extend an Interface from a class in TypeScript ? In this article, we will try to understand how we to extend an interface from a class in TypeScript with the help of certain coding examples. Let us first quickly understand how we can create a class as well as an interface in TypeScript using the following mentioned syntaxes: Syntax:Â This is the s
3 min read
How to Create a Union Type from Nested Array in TypeScript ? TypeScript developers often encounter scenarios where they need to represent a value that could be one of several different types. TypeScript's union types provide a solution to this problem by allowing variables to hold values of multiple types. However, when dealing with nested arrays, each nested
3 min read