Jack Wester
Jack Wester

Reputation: 5650

How do I access get the base type in Typescript?

After trying to grasp how to traverse the prototype chain in the generated Javascript, I failed to implement the function WriteInheritedName() in the example below. The idea is to find the base type and write its name. Any ideas?

class Animal {
}

class Snake extends Animal {
}

function WriteClassName( cls ) {
   console.log( cls.name );
}

function WriteInheritedName( cls ) {
   console.log( "I wish I new how to write Animal" );
}

WriteClassName( Snake );
WriteInheritedName( Snake );

Upvotes: 1

Views: 1324

Answers (1)

Jack Wester
Jack Wester

Reputation: 5650

Found it out myself.

 function WriteInheritedName(cls) {
    console.log( Object.getPrototypeOf(cls.prototype).constructor.name );
 }

Upvotes: 3

Related Questions