Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if class type extends another class? [duplicate]

I'm having a hard time to express my problem, take this following code for exemple.

class Foo {}

class Bar extends Foo {}

const myFct = (bar: typeof Bar) => {
    if(bar instanceof Foo) { 
        // I want to check if Bar extends Foo 
        // bar is not an instance so instanceof won't do it
    }

}

How I can check the class Bar extends Foo from a typeof ?

like image 904
Matthieu Riegler Avatar asked Jul 17 '26 18:07

Matthieu Riegler


1 Answers

try this:

class Foo {}

class Bar extends Foo {}

const myFct = (bar: typeof Bar) => {
    if(bar.prototype instanceof Foo) { 
        // I want to check if Bar extends Foo 
        // bar is not an instance so instanceof won't do it
    }

}
like image 91
Jafar Jabr Avatar answered Jul 20 '26 07:07

Jafar Jabr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!