question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

static property inheritance complaining when it shouldn't

See original GitHub issue

I’ve seen that you’ve already closed many issues on this. But I’m reporting another one in hopes to wear you down 😉

It seems I can’t change the signature of a static function in a derived class. The fact that the subclass is at all aware of the static functions on the super class is really strange.

I developed in C# for years, so I know what you were trying to do… but given that you don’t support a new keyword like C# does, I think this behavior is really wrong. If you’re looking to target being a superset of ES6/ES7, you need to correct this behavior ASAP. Generally in JavaScript, static properties are not copied to subclasses unless the copy is explicit.

I ran into this issue with RxJS Next, where I Subject must inherit from Observable, but they both need different static create() signatures. So I end up hacking around and fighting TypeScript.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:18
  • Comments:21 (7 by maintainers)

github_iconTop GitHub Comments

27reactions
SinoBoeckmanncommented, Jan 25, 2018

So… and now there a plans to change this? I mean, this is pretty old now? Plans for a survey or something?

6reactions
benwerner01commented, Feb 7, 2020

Static factory methods are a useful design paradigm, for instance when object creation relies on data that needs to be asynchronously fetched a static create() method can encapsulate the data fetching, and return the object type (as such fetches cannot be conducted directly in the constructor). This behaviour also aligns with the OOP principles in the popular object oriented languages C# and Java. Requiring that in inheritance each static method with the same name must have compatible types restricts this significantly (for create(), this would enforce the use of the same parameters). Is there an argument for maintaining this restriction?

For example, consider the following:

class Entity {
  id: string;
  name: string;
  constructor(id: string, name: string) {
    this.name = name;
    this.id  = id;
  }

  static async create = (name: string): Promise<Entity> => {
    const { id } = await db.createEntity(name);
    return new Entity(id, name);
  }
}

class Bob extends Entity {
  age: number;
  constructor(id: string, age: number) {
    super(id, 'Bob');
    this.age = age;
  }

  // TS Error: Types of property 'create' are incompatible
  static async create = (age: number): Promise<Bob> => {
    const { id } = await db.createBob(age);
    return new Bob(id, age);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: static properties and inheritance - Stack Overflow
I'm quite new to TypeScript (1.8) and I have a small issue with inheritance and static properties. Please find below the test code...
Read more >
Static Properties in JavaScript Classes with Inheritance
Unfortunately, JavaScript lacks support for static properties, and recommended solutions on Google fail to take into account inheritance.
Read more >
Please stop using classes in JavaScript - everyday.codes
In this article I will talk about why it is a bad idea to use classes in JavaScript, and what are some of...
Read more >
Inheritance, Polymorphism, and Composition in Dart and Flutter
Inheritance is the ability of a class to inherit properties and ... The Dart code analyzer will complain if a non-widget is included....
Read more >
15. Classes - Exploring JS
Complaint: ES6 classes obscure the true nature of JavaScript inheritance; 15.8.2. ... Static properties (or class properties) are properties of Foo itself.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found