Search code examples
javascripttypescriptinstanceoftypeof

Check if class type extends another class?


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 ?


Solution

  • 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
        }
    
    }