I wanted to move to TypeScript from traditional JavaScript because I like the C#-like syntax. My problem is that I can’t find out how to declare static classes in TypeScript.
In C#, I often use static classes to organize variables and methods, putting them together in a named class without needing to instantiate an object. In vanilla JavaScript, I used to achieve this with a simple JavaScript object:
var myStaticClass = {
property: 10,
method: function() {}
}
In TypeScript, I would prefer to use a C#-style approach, but it seems that static classes don’t exist in TypeScript. What is the appropriate solution for declaring a static class in TypeScript?
Hey Ariyashkumar,
Although static classes are not a feature in TypeScript, you can achieve similar functionality with a workaround. By using a private constructor, you can prevent the instantiation of the class from outside. Here’s how you can create a class that behaves similarly to a static class:
class MyStaticClass {
public static readonly property: number = 42;
public static myMethod(): void { /* … / }
private constructor() { / noop */ }
}
This approach allows you to use the class in a way similar to a C# static class. The main limitation is that while you cannot extend classes with private constructors, they can still be instantiated from within the class itself.
TypeScript is different from C#, so you shouldn’t expect to find the exact same concepts in TypeScript. The key question is why you need static classes.
In C#, a static class is a class that cannot be subclassed and contains only static methods. C# does not allow functions to be defined outside of classes. However, in TypeScript, you can define functions and methods outside of classes.
If you want to organize your functions and methods in a namespace (without exposing them globally), you can use TypeScript’s modules. For example:
module M {
var s = “hello”;
export function f() {
return s;
}
}
This way, you can access M.f() externally, but not s, and you cannot extend the module.
Hey Ariyas,
I encountered a similar use case on 31/07/2018 and found a workaround to achieve static-like functionality in TypeScript. Based on my research, this method worked for me. Here’s how you can replicate the behavior of a static class in TypeScript:
var myStaticClass = {
property: 10,
method: function() {}
}
Solution:
Define the Static Members:
Create a file named MyStaticMembers.ts with the following content:
namespace MyStaticMembers {
class MyStaticClass {
static property: number = 10;
static myMethod() { /* … */ }
}
export function Property(): number {
return MyStaticClass.property;
}
export function Method(): void {
MyStaticClass.myMethod();
}
}
Consume the Static Members:
In your main application file, app.ts, use the following code:
///
console.log(MyStaticMembers.Property());
MyStaticMembers.Method();
This approach allows you to use static-like methods and properties through the MyStaticMembers namespace. If anyone has better suggestions or improvements, please share them!