Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access static members from instance methods in typescript?

Tags:

typescript

I try to use a static member from an instance method. I know about accessing static member from non-static function in typescript, but I do not want to hard code the class to allow inheritance:

class Logger {   protected static PREFIX = '[info]';    public log(msg: string) {     console.log(Logger.PREFIX + ' ' + msg); // What to use instead of Logger` to get the expected result?   } }  class Warner extends Logger {   protected static PREFIX = '[warn]'; }  (new Logger).log('=> should be prefixed [info]'); (new Warner).log('=> should be prefixed [warn]'); 

I've tried things like

typeof this.PREFIX 
like image 809
Simon Hürlimann Avatar asked Mar 24 '15 22:03

Simon Hürlimann


People also ask

Can you access a static method with an instance?

A static method cannot access a class's instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated. For the same reason, the this reference cannot be used in a static method.

How do you call a static method in TypeScript?

A static method uses the static keyword instead of the function keyword when we define it. Static members can be encapsulated with the public, private and protected modifiers. We call a static method directly on the class, using the class name and dot notation. We don't need to create an object instance.

Can you access static members from an instance of a class?

The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

Does TypeScript support static classes?

The class or constructor cannot be static in TypeScript.


1 Answers

You simply need ClassName.property :

class Logger {   protected static PREFIX = '[info]';   public log(message: string): void {     alert(Logger.PREFIX + string);    } }  class Warner extends Logger {   protected static PREFIX = '[warn]'; } 

MORE

from : http://basarat.gitbooks.io/typescript/content/docs/classes.html

TypeScript classes support static properties that are shared by all instances of the class. A natural place to put (and access) them is on the class itself and that is what TypeScript does:

class Something {     static instances = 0;     constructor() {         Something.instances++;     } }  var s1 = new Something(); var s2 = new Something(); console.log(Someting.instances); // 2 

UPDATE

If you want it to inherit from the particular instance's constructor use this.constructor. Sadly you need to use some type assertion. I am using typeof Logger shown below:

class Logger {   protected static PREFIX = '[info]';   public log(message: string): void {     var logger = <typeof Logger>this.constructor;      alert(logger.PREFIX + message);    } }  class Warner extends Logger {   protected static PREFIX = '[warn]'; } 
like image 103
basarat Avatar answered Sep 22 '22 03:09

basarat