Skip to content

Proposal: simplify access to static members #3836

@vadimzzz

Description

@vadimzzz

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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions