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

Inheritance and static members #5989

Closed
benzid-wael opened this issue Dec 8, 2015 · 1 comment
Closed

Inheritance and static members #5989

benzid-wael opened this issue Dec 8, 2015 · 1 comment
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@benzid-wael
Copy link

I think that is useful to access to static members :

  • from instances
  • from sub classes

Below an example:

class A {
  protected static type: string;

 public log(): string {
    console.log(this.type);
  }
}

class B extends A {
  protected static type: string = 'C';
}

class C extends A {
  protected static type: string = 'C';
}

b = new B();
b.log(); // => 'B'

c = new C();
c.log(); // => 'C'
@ahejlsberg
Copy link
Member

We can't do it the way you suggest since this.type is used to access an instance property named type, not a static property.

However, your example works if you write it as follows:

class A {
  "constructor": typeof A;  // Explicitly declare constructor property
  protected static type: string;

  public log() {
    console.log(this.constructor.type);  // Access actual constructor object
  }
}

class B extends A {
  protected static type: string = 'B';
}

class C extends A {
  protected static type: string = 'C';
}

let b = new B();
b.log(); // => 'B'

let c = new C();
c.log(); // => 'C'

By default the constructor property of an instance is of type Function, but you can specialize it in a class by manually declaring it. Once you do that you can access the statics through this.constructor.xxx, and you'll get the properties of the constructor object that actually created the instance (i.e. the derived constructor object).

@ahejlsberg ahejlsberg added the Question An issue which isn't directly actionable in code label Dec 9, 2015
@DanielRosenwasser DanielRosenwasser added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision and removed Question An issue which isn't directly actionable in code labels Dec 9, 2015
@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
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants