首页 > 解决方案 > 创建一个类别名来调用静态方法

问题描述

如何正确定义类的别名并使用该别名调用一些静态方法?

假设我有这门课


class theClassThatIWantToAlias{
    static theStaticMethod(){
        console.log("called")
    }
}

/* I can call the statc method doing so */
theClassThatIWantToAlias.theStaticMethod();

我想创建一个别名,以便能够将该方法调用为

theClassThatIWantToAlias -> myAlias;
myAlias.theStaticMethod();

我想过做这件事

/* 1) define a type from the class */
type alias = theClassThatIWantToAlias;
alias.theStaticMethod();

/* 2) define an interface from the class */
interface alias extends theClassThatIWantToAlias {}
alias.theStaticMethod();

/* 3) Define a new class that extend the main one */
class alias extends theClassThatIWantToAlias {} 
alias.theStaticMethod();

我很确定前两个是没有意义的,我想知道第三个是否是完成这项任务的“正确”方式。任何想法?

标签: javascripttypescript

解决方案


您既不想声明类型或接口,也不想定义新类。您要做的就是引入另一个包含相同类对象的变量:

const Alias = TheClassThatIWantToAlias;

推荐阅读