Typescript: Call static method without repeating the class name

Created on 24 Mar 2016  路  9Comments  路  Source: microsoft/TypeScript

When I create a static method, the only way to call it seems to be through the actual class name: MyClass.staticMethod. It would be great if there was a way to call the static method without repeating the class name, it would be less error prone when you later decide to rename the class.

A few suggestions:
self.class.staticMethod or
self.type.staticMethod or
Self.staticMethod

In other languages you can do it like this:

  • PHP: $this->staticMethod(); or $this::staticMethod();
  • Python: self.__class__.staticMethod()
  • Ruby: self.class.staticMethod
  • Objective-C: [[self class] staticMethod]
  • Swift: self.dynamicType.staticMethod()
Question

Most helpful comment

You can walk to most places instead of driving there, that is not really a reason for not using a car.

All 9 comments

A good IDE should be able to rename/refactor your class without any problem, especially if the refactor is in the same class.

You can walk to most places instead of driving there, that is not really a reason for not using a car.

You should be able to write this.staticMethod in a static context of the same class.

+1

Not to mention Java where you can call a static method just by it's name (from within the same class of course).

This will make the code much more readable.

@DanielRosenwasser it is usual for me to call static methods/props from instance methods, but this points to the instance side. this.constructor._property says _"Property '_property' does not exist on type 'Function'."_ Is it difficult to implement reference to static side of the class?

@bloadvenro +1
Had a problem with this today.

+1

+1
This would be a really nice to have feature when you are dealing with really long classNames and does not have the option of changing the class name, especially when you are in a ternary along with x max-line-length

In my opinion, this feature is really important to improve the readability of the code. Although the ability to refactor the code more easily is also welcomed, of course.

When you have to add a class name before a method call, a statement that usually takes 1 line, takes 2 (or even more). Specially if the names of your class, method or arguments (or all them) are not trivial, and you've to restrict the line length to 80 characters.

With regard to the implementation of this feature, I would love to be able to omit the class name entirely, as in Java. But I guess that it could lead to confusion in JavaScript; because one wouldn't know if the code is calling a static method or an independent function.

But that already may happen with instance methods, if you set the option noImplicitThis to false. Doesn't it? For that cases, I guess that editors and IDEs should allow to use a different colour for the calls to functions.

I also like the idea of the self keyword. But I wonder if it would cause problems with code that already uses that word. I guess that the TS developers could create an option, called referencesToSelfStatic or refsToSelfStatic, with the following possible values:

  • explicit: only allows the use of the class name.
  • self: allows the use of the class name or the self keyword (for a given statement).
  • implicit: allows the use of the class name or the self keyword, or their omission (for a given statement).

These values could also be named as: className, className+self, className+self+implicit.

This way, we all could choose the option that better suited our needs.

Was this page helpful?
0 / 5 - 0 ratings