Effect CommunityEC
Effect Community6mo ago
1 reply
Stef

Creating TypeScript classes where static properties are automatically available as instance prope...

I have been trying this meta-programming puzzle for a while now, and could not manage to create an ergonomic solution with clean api surface. Maybe some experts around here have a good solution?

Problem:

I am trying to configure class instance properties through static properties. The child class should have its static properties of type Foo also available as instance properties, with the generic type of T of Foo.

In javascript I can make this work, no problem, but the challenge is here is making it type safe.

Expected outcome:

class Foo<T> {}

class Base {
  constructor() {
    // this is where the magic happens
  }
}

class Child extends Base {
  static someValue = new Foo<string>();

  constructor() {
    super();
    this.someValue; // string
  }
}
Was this page helpful?