1

TypeScript doesn't seem to do a very good job of separating namespace from static properties.

class ClassA {
  static ClassB = class {
  };
}

type T = ClassA // ok

type T = ClassA.ClassB // error: 'ClassA' only refers to a type, but is being used as a namespace here.

new ClassA.ClassB() // ok

In the above example I want the code to not report errors.

  • `Foo.Type` is a variable exists in runtime space. It does not exist in type space. – Ricky Mo Jun 23 '23 at 09:13
  • Welcome to Stack Overflow! What specific type do you want `T` to be? Maybe you want it to be `(typeof Foo)["Type"]` to be the type of the `Type` constructor? Or maybe you want it to be `InstanceType<(typeof Foo)["Type"]>` to be the type of a `Type` instance? Or something else? Given that there is no type named `Foo.Type`, and you haven't shown how you want to use `T`, we just have to guess. Please [edit] to clarify what you want `T` to resolve to (preferably with example usages) and then we can answer. Good luck! – jcalz Jun 23 '23 at 13:06

2 Answers2

0

If you want your class to be a namespace, merge it with a namespace

class Foo {}
namespace Foo {
   export class Type {}
}

type T = Foo.Type // ok
new Foo.Type() // ok

Classes (unlike namespaces) can't have static types on them

Dimava
  • 7,654
  • 1
  • 9
  • 24
0

To circumvent the syntax of namespace in typescript, the following scheme is used instead:


class ClassA {
  static ClassB = class {
  };
}


type T = InstanceType<typeof ClassA.ClassB> // ok

// or
type T = typeof ClassA.ClassB.prototype // ok


new ClassA.ClassB() // ok