To add a private static property to a class in TypeScript, you can use the private
and static
keywords along with the property name and its type. Here is an example:
index.ts68 chars4 lines
In this example, myPrivateStaticProperty
is a private static property of MyClass
with the type string
. This property can only be accessed within the class and cannot be accessed from outside the class or its instances.
You can also initialize the private static property with a value:
index.ts86 chars4 lines
In this case, myPrivateStaticProperty
is initialized with the value 'default value'
.
To access the private static property within the class, you can use the MyClass.myPrivateStaticProperty
syntax:
index.ts156 chars8 lines
In this example, myMethod
logs the value of myPrivateStaticProperty
to the console.
gistlibby LogSnag