Petah
Petah

Reputation: 46060

How can I type hint a class, rather than an instance of the class?

How can I type hint a class, rather than an instance of the class?

I tried this, but I get Cannot use 'new' with an expression whose type lacks a call or construct signature.

class Foo {
}

class Bar extends Foo {
}

class Baz extends Foo {
}

function test(c: Foo) {
    new c();
}

test(Baz);

Upvotes: 3

Views: 815

Answers (1)

basarat
basarat

Reputation: 276115

Use typeof:

class Foo {
}

class Bar extends Foo {
}

class Baz extends Foo {
}

function test(c: typeof Foo) {
    new c();
}

test(Baz);

Upvotes: 6

Related Questions