TypeScript:继承类中静态方法的自引用返回类型

时间:2015-12-04 21:47:35

标签: generics inheritance static typescript self-reference

在TypeScript 1.7中使用Polymorphic this,正如我发现here,我们可以在类中定义一个返回类型为this的方法,并自动定义任何扩展该类的类并继承方法,将其返回类型设置为各自的this类型。像这样:

class Model {
  save():this {    // return type: Model
    // save the current instance and return it
  }
}

class SomeModel extends Model {
  // inherits the save() method - return type: SomeModel
}

然而,我所追求的是拥有一个继承的static方法,其返回类型引用了类本身。最好用代码描述:

class Model {
  static getAll():Model[] {
    // return all recorded instances of Model as an array
  }

  save():this {
    // save the current instance and return it
  }
}

class SomeModel extends Model {
  // inherits the save() method - return type: SomeModel
  // also inherits getAll() - return type: Model (how can we make that SomeModel?)
}

也许我不得不考虑采用不同的方式来实现这一点,因为TypeScript 1.7中的多态this不支持设计static方法

编辑:我想我们会看到这个Github问题是如何结束的:https://github.com/Microsoft/TypeScript/issues/5863

4 个答案:

答案 0 :(得分:17)

这在TypeScript 2.0+中是可行的。通过使用内联Tcl/expect tutorials类型来捕获{ new(): T },您将获得所需内容:

this

请注意,class BaseModel { static getAll<T>(this: { new(): T }): T[] { return [] // dummy impl } save(): this { return this // dummy impl } } class SubModel extends BaseModel { } const sub = new SubModel() const savedSub: SubModel = sub.save() const savedSubs: SubModel[] = SubModel.getAll() 仍然期望此类型没有参数。

有关详细信息,请参阅https://www.typescriptlang.org/docs/handbook/generics.html#using-class-types-in-genericshttps://stackoverflow.com/a/45262288/1268016

答案 1 :(得分:13)

基于simplest answerGitHub issue,您可以像这样使用InstanceType<>

class Foo {
    static create<T extends typeof Foo>(this: T): InstanceType<T> {
        return new this() as InstanceType<T>
    }

    static getAll<T extends typeof Foo>(this: T): Array<InstanceType<T>> {
        return []
    }
}

class Bar extends Foo { }

const a = Bar.getAll() // typeof a is Bar[]
const b = Bar.create() // typeof b is Bar.

我从链接的GitHub示例中插入了create函数,只是为了进行说明。

答案 2 :(得分:0)

您希望这个静态方法在继承的子类中返回什么?它是这样的:

class A {
    private static _instances = new Array<A>();
    static instances(): A[] { return A._instances; }
    constructor() { A._instances.push(this); }
}

class B extends A {

    static instances(): B[] { 
        return <B[]>(A.instances().filter(i => i instanceof B)); 
    }
    constructor() { super(); }; 
}

var a = new A();
var b = new B();

console.log(
     "A count: " + A.instances().length + 
    " B count: " + B.instances().length);

这将输出&#34; A计数:2 B计数:1&#34;。或者你期待什么?

答案 3 :(得分:0)

基于Brains answer,您可以直接获得返回类型,只需

(typeof Class)['foo']

示例:

export class MyClass {
    myFunction(param:string):number{
        return typeof param === "string"?1:0
    }
                                             /** Just class['foo'] to normal methods */   
    static myStaticFunctions(cards: string): Parameters<MyClass['MyFunction']>[0] {
                        /** and *(typeof class)* ['foo'] to static methods */   
        const typedReturn: ReturnType<(typeof MyClass)['myStaticFunctions']> = "works"
        return typedReturn
        
    }
}