Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: simplify access to static members #3836

Closed
vadimzzz opened this issue Jul 13, 2015 · 6 comments
Closed

Proposal: simplify access to static members #3836

vadimzzz opened this issue Jul 13, 2015 · 6 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@vadimzzz
Copy link

When accessing static member of class you must write class name followed with dot before member name. But this is redundant when using that member inside the class itself.
Let's see an example (it's a little bit far-fetched but shows the case):

class SampleClassWithLongName {
    static SCALE = 5;
    private num:number;

    // ...

    getScaled():number {
        return SampleClassWithLongName.scaleNumber(this.num);
    }

    static scaleNumber(n:number):number {
        return n * SampleClassWithLongName.SCALE;
    }
}

My suggestion is to extend the functionality of static keyword (just to not introduce a new one) so it will help eliminate the need to continuously repeat class name.
With this feature our example became shorter and more readable:

class SampleClassWithLongName {
    static SCALE = 5;
    private num:number;

    // ...

    getScaled():number {
        return static.scaleNumber(this.num);
    }

    static scaleNumber(n:number):number {
        return n * static.SCALE;
    }
}
@OlegDokuka
Copy link

+1

or

this::staticValue

@danquirk danquirk added the Suggestion An idea for TypeScript label Jul 13, 2015
@saschanaz
Copy link
Contributor

this.constructor.scaleNumber will work on methods while not on static functions.

@moonpyk
Copy link

moonpyk commented Sep 21, 2015

+1

@mhegazy mhegazy added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Dec 9, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2016

The class name does not seem like that bad of an option, and does not justify the complexity of adding a new syntax to the language.

@mhegazy mhegazy closed this as completed Feb 22, 2016
@mhegazy mhegazy added the Declined The issue was declined as something which matches the TypeScript vision label Feb 22, 2016
@aiboost
Copy link

aiboost commented Oct 27, 2016

mhegazy, accessing static property via class is not the same as accesing via 'this'.
If you access

class Parent {
  static x: 0;
  public method(): number {  
    return Parent.x;
  }
}

all child classes (Child extends Parent) will use x = 0 even if they redefined it.
So accessing by 'this' is very useful, please give a chance to 'this.x' syntax.

@leegee
Copy link

leegee commented Nov 21, 2017

How hard would it be to user super as a reference to the super class?

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

8 participants