2

Is it possible to call static method of type parameter? For example, I have the following code:

class BaseA1 {
  public static getString(): string {
    return "A1";
  }
}

class A<A1 extends BaseA1> {
  constructor() {
    var name = A1.getString(); // <== error [the property getString does not exist ...]
  }
}

How to call static method correctly? Is it possible at all, if not what might be the best alternative way? Thanks, kindly.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Andrei Tarutin
  • 684
  • 1
  • 10
  • 25
  • It's a fine balance, but as this relates purely to TypeScript's classes stuff and not at all to JavaScript, I've removed the `javascript` tag. (Many, many TypeScript questions appropriately also have the `javascript` tag, but when the question is purely about a TypeScript language feature, it's not appropriate.) – T.J. Crowder Jun 19 '14 at 15:46
  • Good question, btw, and well-asked. – T.J. Crowder Jun 19 '14 at 15:58

1 Answers1

2

Static methods belong to the class type, not to an instance. In particular, if A1 defined it's own getString, it would not be an overridden method, but a shadowed method.

So the only scenario in which what you want to do makes sense is when you want to rely on a type shadowing a method from its base type. This, however, would be very smelly code (in other words: don't do it).

Either call BaseA1.getString() or, if you need a way to override it in a subclass, have it be a proper instance method. Seeing that your example returns the name of the subclass, I tend to think you are trying to achieve something fishy and would suggest you ask another question on what the underlying problem is that you want to solve rather than asking a question on how to make the current design work.

You can find an explanation on this here; it's about Java rather than Typescript, but the idea is the same.

Community
  • 1
  • 1
Ingo Bürk
  • 19,263
  • 6
  • 66
  • 100
  • tldr; the static belongs to BaseA1, a known type, so why call from A1? – x0n Jun 19 '14 at 15:51
  • @x0n: In case it's shadowed by the concrete type being used, since `constructor` is instance-specific and that concrete type is known. The question makes perfect sense, and there's an argument for handling it (again, only in instance-specific code). TypeScript apparently doesn't, nor does Java (Java would compile it, but call BaseA1's version), but at a high level the question is quite reasonable (more reasonable in Typescript than Java perhaps, as TypeScript is more dynamic, but probably reasonable in both). – T.J. Crowder Jun 19 '14 at 15:57
  • That is coinsidence (talking about name of subclass). The return string could have been of any content. Probably, I am writing a model layer of MVC pattern, each class represents an entity. The root parent class of all entities has a static method which returns the name of the table in database. This method should be overriden in descedants. That is the case. – Andrei Tarutin Jun 19 '14 at 16:04
  • @AndrewTarutin: (Not sure what coincidence you're referring to...) Yeah, perfectly valid use case (and that's the reason this question shows up in regard to other languages with generics, like Java). I don't think you have much option, you'll have to go with Ingo's suggestion of using an instance method, even though it's not instance-specific information. (Shouldn't add any real overhead, so not a painful option...) – T.J. Crowder Jun 19 '14 at 16:09
  • It could have been a possible solution. So be it. Thanks! – Andrei Tarutin Jun 19 '14 at 16:14
  • @T.J. Crowder, I think that is [the possible solution](http://tinyurl.com/qbljqpz) Ingo was talking about – Andrei Tarutin Jun 19 '14 at 16:40
  • @AndrewTarutin: Thanks, I was being an idiot. I really need to do *one thing at a time*. :-) – T.J. Crowder Jun 19 '14 at 16:48