SilveryStar
SilveryStar

Reputation: 101

Can I remove `typeof` operator in type

I have a problem about type manipulation

class A {}
class B extends A {}
class C extends A {}

function exampleFn1<T extends Record<string, A>>( t: T ) {
    for( const v of Object.values( t ) )
        new v();   // TS2351: This expression is not constructable.    
                   // Type 'A' has no construct signatures.
}

function exampleFn2<T extends Record<string, typeof A>>( t: T ) {
    for( const v of Object.values( t ) )
        new v();   // ok
}

Although there is no error by the second method, I want to get A instead of typeof A, how can I do

type A = typeof Example;

type B = RemoveTypeof<A>; // Expect type B = Example

Upvotes: 1

Views: 880

Answers (1)

GOTO 0
GOTO 0

Reputation: 47682

Use InstanceType:

type B = InstanceType<A>;

or simply

type B = A["prototype"];

Demo link

Upvotes: 3

Related Questions