简体   繁体   English

从打字稿中的通用类型调用通用类型的静态函数

[英]Call static function of Generic Type from Generic Type in typescript

I have a function in a service class that is passed a generic type extending my Bean class. 我在服务类中有一个函数,该函数传递了扩展我的Bean类的通用类型。 I want the function to call a static function in the Bean class to create an array of Beans. 我希望函数在Bean类中调用静态函数来创建Bean数组。 My code in the playground compiles, however I get a runtime browser error of 我在操场上的代码可以编译,但是出现运行时浏览器错误

Uncaught TypeError: ctor.constructor.fromEntryList is not a function

If I remove the .constructor I get the array of beans created, however there is a typescript error without .constructor . 如果删除.constructor得到创建的bean数组,但是如果没有.constructor ,则会出现打字稿错误。

I am lost at how to correctly typescript code this function. 我不知道如何正确键入此功能的脚本代码。 Any help is much appreciated as I have unsuccessfully found a prior answer on SO. 非常感谢任何帮助,因为我未能找到关于SO的先前答案。

Code: 码:

// Base class that will be extended
class Bean {
    id: number;

    static fromEntryList<T1>(ctor: new (p: number) => T1, numbers: Array<number>): Array<T1> {

        let result: Array<T1> = [];
        for (let anumber of numbers) {
            result.push(new ctor(anumber));
        }
        return result;
    }


    constructor(p: number) {
        this.id = p;
    }
}

// Base service class that will be extended
class BeanService {


    constructor() { }

    get_entry_list<T extends Bean>(ctor: new (p: number) => T): T[] {
        let x: T[] = (<typeof Bean>ctor.constructor).fromEntryList(ctor, [1, 2, 3, 4]);
        return x;
    }
}

let service = new BeanService();
let beans: Bean[] = service.get_entry_list(Bean);

code in playground 操场上的代码

Thanks. 谢谢。

in your param definition in get_entry_list , it only defines type of ctor but missing static method interface, so accessing it raises compiliation error while runtime can access those property correctly works. get_entry_list的param定义中,它仅定义ctor的类型,但缺少静态方法接口,因此,在运行时可以正确访问这些属性的同时,对其进行访问会引发编译错误。

You may able to define constructor interface, 您可能可以定义构造函数接口,

interface XXX<T> {
  new(p: number): T;
  fromEntryList<U>(ctor: new (p: number) => U, numbers: Array<number>): Array<U>;
}

which is basically representation of Bean then use it for param's signature 这基本上是Bean表示形式,然后将其用于param的签名

get_entry_list<T extends Bean>(ctor: XXX<T>): Array<T>

allows ctor supplied in get_entry_list can access static method. 允许get_entry_list中提供的ctor可以访问静态方法。

playground: 操场:

https://www.typescriptlang.org/play/#src=%2F%2F%20Base%20class%20that%20will%20be%20extended%0D%0Aclass%20Bean%20%7B%0D%0A%20%20id%3A%20number%3B%0D%0A%0D%0A%20%20static%20fromEntryList%3CT1%3E(ctor%3A%20new%20(p%3A%20number)%20%3D%3E%20T1%2C%20numbers%3A%20Array%3Cnumber%3E)%3A%20Array%3CT1%3E%20%7B%0D%0A%0D%0A%20%20%20%20%20%20let%20result%3A%20Array%3CT1%3E%20%3D%20%5B%5D%3B%0D%0A%20%20%20%20%20%20for%20(let%20anumber%20of%20numbers)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20result.push(new%20ctor(anumber))%3B%0D%0A%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20return%20result%3B%0D%0A%20%20%7D%0D%0A%0D%0A%0D%0A%20%20constructor(p%3A%20number)%20%7B%0D%0A%20%20%20%20%20%20this.id%20%3D%20p%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Ainterface%20XXX%3CT%3E%20%7B%0D%0A%20%20new(p%3A%20number)%3A%20T%3B%0D%0A%20%20fromEntryList%3CU%3E(ctor%3A%20new%20(p%3A%20number)%20%3D%3E%20U%2C%20numbers%3A%20Array%3Cnumber%3E)%3A%20Array%3CU%3E%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20Base%20service%20class%20that% https://www.typescriptlang.org/play/#src=%2F%2F%20Base%20class%20that%20will%20be%20extended%0D%0Aclass%20Bean%20%7B%0D%0A%20%20id% 3A%20number%3B%0D%0A%0D%0A%20%20static%20fromEntryList%3CT1%3E(ctor%3A%20new%20(p%3A%20number)%20%3D%3E%20T1%2C%20numbers %3A%20Array%3Cnumber%3E)%3A%20Array%3CT1%3E%20%7B%0D%0A%0D%0A%20%20%20%20%20%20let20let%20result%3A%20Array%3CT1% 3E%20%3D%20%5B%5D%3B%0D%0A%20%20%20%20%20%20for%20(let%20anumber%20of%20numbers)%20%7B%0D%0A%20 %20%20%20%20%20%20%20%20%20result.push(new%20ctor(anumber))%3B%0D%0A%20%20%20%20%20%20%20%7D%0D %0A%20%20%20%20%20%20return%20result%3B%0D%0A%20%20%7D%0D%0A%0D%0A%0D%0A%20%20constructor(p%3A%20number )%20%7B%0D%0A%20%20%20%20%20%20this.id%20%3D%20p%3B%0D%0A%20%20%7D%0D%0A%7D%0D% 0A%0D%0Ainterface%20XXX%3CT%3E%20%7B%0D%0A%20%20new(p%3A%20number)%3A%20T%3B%0D%0A%20%20fromEntryList%3CU%3E(ctor %3A%20new%20(p%3A%20number)%20%3D%3E%20U%2C%20numbers%3A%20Array%3Cnumber%3E)%3A%20Array%3CU%3E%3B%0D%0A%7D %0D%0A%0D%0A%2F%2F%20Base%20service%20class%20that% 20will%20be%20extended%0D%0Aclass%20BeanService%20%7B%0D%0A%0D%0A%0D%0A%20%20constructor()%20%7B%20%7D%0D%0A%0D%0A%20%20get_entry_list%3CT%20extends%20Bean%3E(ctor%3A%20XXX%3CT%3E)%3A%20Array%3CT%3E%20%7B%0D%0A%20%20%20%20%20%20let%20x%3A%20T%5B%5D%20%3D%20ctor.fromEntryList(ctor%2C%20%5B1%2C%202%2C%203%2C%204%5D)%3B%0D%0A%20%20%20%20%20%20return%20x%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Alet%20service%20%3D%20new%20BeanService()%3B%0D%0Alet%20beans%3A%20Bean%5B%5D%20%3D%20service.get_entry_list(Bean)%3B%0D%0A%0D%0Aconsole.log(beans)%3B 20will%20be%20extended%0D%0Aclass%20BeanService%20%7B%0D%0A%0D%0A%0D%0A%20%20constructor()%20%7B%20%7D%0D%0A%0D%0A% 20%20get_entry_list%3CT%20扩展%20Bean%3E(ctor%3A%20XXX%3CT%3E)%3A%20Array%3CT%3E%20%7B%0D%0A%20%20%20%20%20%20%20let %20x%3A%20T%5B%5D%20%3D%20ctor.fromEntryList(ctor%2C%20%5B1%2C%202%2C%203%2C%204%5D)%3B%0D%0A%20% 20%20%20%20%20return%20x%3B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Alet%20service%20%3D%20new%20BeanService()% 3B%0D%0Alet%20beans%3A%20Bean%5B%5D%20%3D%20service.get_entry_list(Bean)%3B%0D%0A%0D%0Aconsole.log(beans)%3B

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM