L
L
lightalex2020-10-15 17:49:24
typescript
lightalex, 2020-10-15 17:49:24

How to correctly write the type of the argument so that the static properties of the class are visible?

There is the following code:

class A {
  static b = 123;
}
function f(cls: new() => A) {
  console.log(cls.b); // Ошибка: Свойство "b" не существует в типе "new () => A".
}

Can you tell me how to make TypeScipt understand that clsI want to get a class Ain, along with static properties?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MagnusDidNotBetray, 2020-10-15
@MagnusDidNotBetray

So?

class A {
  public static b: number = 123;
}
function f<T extends {b: number, new (...args: any[]): InstanceType<T>}>(cls: T) {
  console.log(cls.b);
}

A
Aetae, 2020-10-16
@Aetae

class A {
  static b = 123;
}
function f(cls: typeof A) {
  console.log(cls.b); 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question