How to access static members from instance methods in typescript?
Asked Answered
L

1

35

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
Loggins answered 24/3, 2015 at 22:31 Comment(1)
TLDR - ClassName.property. For overridden properties, (<typeof ClassName> this.constructor).property.Dick
Q
45

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 : https://basarat.gitbook.io/typescript/future-javascript/classes

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]';
}
Quadrangle answered 24/3, 2015 at 22:42 Comment(9)
I know about using the ClassName.property. But then it will use Logger.PREFIX even in subclasses that have their own .PREFIX defined.Aiello
Updated the question to be more clear about the expected result.Aiello
@SimonHürlimann Updated the answer to take that into account ;)Quadrangle
It seems I can use <typeof Logger> instead of <any> in the typecast. This seems more appropriate.Aiello
@SimonHürlimann I knew I was being lazy and hence left ambiguity with some. Thanks for clearing that up. Example updated for future peeps :)Quadrangle
Any way to make this work if Logger.log is static? I would really need that behavior, but can't seem to find out how to do it.Broadminded
This does not work. For example: ``` const a = new Logger(); a.log('a ') const b = new Warner(); b.log('b ') ``` Both log InfoGiffy
Why this answer is voted up? Warner instance printed out [info]Leakey
@JimmyKane: Your example works correctly for me (after changing alert() to console.log()) with node v16.17.0, TS v4.4.2.Britain

© 2022 - 2024 — McMap. All rights reserved.