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
How to declare a module in TypeScript ? A module is a piece of code that can be called or used in another code. There is nothing new about modules in Typescript. The concept of the module was introduced by JavaScript with ECMAScript 2015 release. Typescript is just re-using this feature.Will the code not work without Modules?Of course, it
7 min read
What are type aliases and how to create it in Typescript ? In Typescript, Type aliases give a type a new name. They are similar to interfaces in that they can be used to name primitives and any other kinds that you'd have to define by hand otherwise. Aliasing doesn't truly create a new type; instead, it gives that type a new name. Aliasing a primitive isn't
3 min read
How to Create Arrays of Generic Interfaces in TypeScript ? In TypeScript, managing data structures effectively is crucial for building robust applications. Arrays of generic interfaces provide a powerful mechanism to handle varied data types while maintaining type safety and flexibility. There are various methods for constructing arrays of generic interface
3 min read
How to use Interface with Class in TypeScript ? In TypeScript, interfaces define the structure that classes must adhere to, ensuring consistent object shapes and facilitating type-checking.Interfaces declare properties and methods without implementations, serving as contracts for classes to implement.Classes use the implements keyword to adhere t
3 min read
How to Implement a TypeScript Decorator ? In TypeScript, decorators are a feature that allows you to attach metadata to classes, methods, properties, or parameters concisely and expressively. TypeScript decorators are applied using the @decorator syntax. In this article, we are going to learn how to implement a typescript decorator. How to
2 min read