【问题标题】:Check if class type extends another class? [duplicate]检查类类型是否扩展了另一个类? [复制]
【发布时间】:2022-01-19 23:46:21
【问题描述】:

我很难表达我的问题,以下面的代码为例。

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
    }

}

如何从 typeof 检查类 Bar extends Foo ?

【问题讨论】:

标签: javascript typescript instanceof typeof


【解决方案1】:

试试这个:

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
    }

}

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多