将静态方法定义为返回当前类的实例

时间:2015-05-24 23:10:08

标签: typescript

我想做这样的事情:

class Base {
   static foo(): <???>; // <= What goes here?
}

class Subclass extends Base {}

Subclass.foo() // <= I want this to have a return type of Subclass, not Base

2 个答案:

答案 0 :(得分:5)

TypeScript在过去一年中一直在积极添加功能,包括对I am doing this data structure program and it showing a error : expected expression ')' token at line 33. i don't have any idea how to get over this.的支持。 using class types in generics在2.3或更高版本中编译:

with_items: ec2.instances

答案 1 :(得分:3)

遗憾的是,您需要明确地注释或以编译器可以推断它的方式编写:

class Base {
   static foo() {
       return new Base();
   } 
}

class Subclass extends Base {
    static foo() {
       return new Subclass();
    }
}

Subclass.foo()

在任何一种情况下,Subclass都需要某种形式的重复定义。