简体   繁体   English

Typescript调用泛型类型的静态方法

[英]Typescript call static method of generic type

I have a service which I want to use to return a object of a specified type build from a JSON. 我有一个服务,我想用它来从JSON返回指定类型构建的对象。

I have a class MyClass which implements a static class which define a FromJSON static method. 我有一个MyClass类,它实现了一个定义FromJSON静态方法的静态类。

export interface InterfaceMyClass {
    static FromJSON(json: any): any;
}

export class MyClass implements InterfaceMyClass {

    constructor(){}

    FromJSON(json: any): MyClass {
        let instance = MyClass.create(MyClass.prototype);
        Object.assign(instance, json);
        // Some other code specific to MyClass
        return instance;
    }
}

I don't know how to call the static method of the generic class that I've passed in parameter in my service. 我不知道如何调用我在服务中传递参数的泛型类的静态方法。 My service looks like this: 我的服务如下:

export class MyService<T extends InterfaceMyClass> {
    getObject() {
        let json = getExternalJson(...);
        return T.FromJSON(json); // <-- How to call static method FromJSON from T class ?
    }
}

I would like to use the service this way: 我想用这种方式使用服务:

let service = new MyService<MyClass>();
let myObject = service.getObject(); // <-- Should by an MyClass instance (created by MyClass.FromJSON)

Question: How can I call this T.FromJSON method? 问题:如何调用此T.FromJSON方法?

Bonus question: What is the good way to implements static method? 奖金问题:实现静态方法的好方法是什么? I dont think that my FromJSON method from MyClass is static. 我不认为我的MyClass FromJSON方法是静态的。 If I add static word before FromJSON , it tells me this: 如果我在FromJSON之前添加静态单词,它会告诉我:

[ts] Class 'MyClass' incorrectly implements interface 'InterfaceMyClass'.
       Property 'FromJSON' is missing in type 'MyClass'.

A few things: 一些东西:

(1) Interfaces can't have static declarations, for example: (1)接口不能有静态声明,例如:

interface MyInterface {
    static myMethod(); // Error: 'static' modifier cannot appear on a type member
}

( code in playground ) 游乐场代码

The way to get around this in typescript is to define a builder/constructor interface: 在打字稿中解决这个问题的方法是定义一个构建器/构造函数接口:

interface MyInterface {}

interface MyInterfaceBuilder {
    new (): MyInterface;
    myMethod();
}

(2) Generic constraints are only available in compilation time, but then the compiler removes them as javascript doesn't support it, for example: (2)通用约束仅在编译时可用,但编译器会删除它们,因为javascript不支持它,例如:

class MyClass<T extends string> {
    private member: T;

    constructor() {
        this.member = new T(); // Error: Cannot find name 'T'
    }
}

compiles into: 编译成:

var MyClass = (function () {
    function MyClass() {
        this.member = new T(); // Error: Cannot find name 'T'
    }
    return MyClass;
}());

( code in playground ) 游乐场代码

When looking at the js output it's clear why the compiler throws the Cannot find name 'T' error, as T is nowhere to be found. 当查看js输出时,很清楚为什么编译器会抛出Cannot find name 'T'错误,因为T无处可寻。

To get around all of that, here's a solution for you: 为了解决所有问题,这里有一个解决方案:

interface IMyClass {}

interface IMyClassBuilder<T extends IMyClass> {
    new (): T;
    FromJSON(json: any): any;
}

class MyClass implements IMyClass {
    static FromJSON(json: any) {
        return "";
    }
}

class MyService<T extends IMyClass> {
    private classToCreate: IMyClassBuilder<T>;

    constructor(classToCreate: IMyClassBuilder<T>) {
        this.classToCreate = classToCreate;
    }

    getObject(): T {
        let json = getExternalJson(...);
        return this.classToCreate.FromJSON(json);
    }
}

let service = new MyService(MyClass);
let myObject = service.getObject();

( code in playground ) 游乐场代码

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

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