when function expression to declare class how can I define type?

38 Views Asked by At
const Foo: new () => any = class {
  constructor() {}
  bar(): string {
    return ‘Hello World!’;
  }
};
const instance = new Foo();

below I want to replace any cause my config not allows any

new () => any

How can I define Foo()'s result ?

1

There are 1 best solutions below

1
On

I suggest defining an interface named Foo to correspond to the instances that the class expression named Foo creates:

interface Foo {
  bar(): string;
}

Then you can declare Foo as type new () => Foo:

const Foo: new () => Foo = class {
  constructor() { }
  bar(): string {
    return 'Hello World!';
  }
};

Hope that helps; good luck!

Playground link to code