打字稿:polymorhpic这个,重载签名与函数实现不兼容

时间:2017-09-14 22:57:32

标签: typescript

有人可以解释为什么这不起作用:

abstract class Model {
    static deserialize<T extends Model>(this: ( new() => T ), object: any): T;
    static deserialize<T extends Model>(ctor: ( new() => T ), object: any): T {
        return new ctor();
    }

    static deserializeArray<T extends Model>(this: ( new() => T ), ...objects: any[]): T[];
    static deserializeArray<T extends Model>(ctor: ( new() => T ), ...objects: any[]): T[] {
        return objects.map(object => Model.deserialize(ctor, object));
    }
}

class MyModel extends Model { }

允许:

let myModel = MyModel.deserialize({});
let myModels = MyModel.deserializeArray({}, {}, {});

let myModel = Model.deserialize(MyModel, {});
let myModels = Model.deserializeArray(MyModel, {}, {}, {});

Typescript 2.5.2抱怨“重载签名与函数实现不兼容”。

为什么需要这两种形式?

考虑一个返回序列化(json)模型的REST API:

class MyModelController {
    get(id: number) {
        let myModel = ... some db/service call ...
        return myModel.serialize();
    }
}

然后是一个通用服务(角度)来请求模型:

@Injectable()
abstract class HttpService {
    constructor(private http: Http) { }

    errorHandler(response) {
        ...
    }

    get<T extends Model>(ModelType: ( new() => T ), endpoint: string): Observable<T> {
        return this.http.get(endpoint)
            // we can't call ModelType.deserialize() here...
            .map(response => Model.deserialize(ModelType, response.json()))
            .catch(response => this.errorHandler(response));
    }
}

@Injectable()
class MyModelService extends HttpService {
    get(id: number) {
       return super.get(MyModel, `/api/models/${id}`);
    }
}

解决方案

abstract class Model {
    static deserialize<T extends Model>(this: ( new() => T), object: {}): T;
    static deserialize<T extends Model>(this: Function & { prototype: Model }, ctor: ( new() => T ), object: {});
    static deserialize<T extends Model>(this: ( new() => T), first: ( new() => T ) | {}, second?: any) {
        return typeof first === "function" ? new first() : new this(); 
    }

    static deserializeArray<T extends Model>(this: ( new() => T), ...objects: {}[]): T[];
    static deserializeArray<T extends Model>(this: Function & { prototype: Model }, ctor: ( new() => T ), ...objects: {}[]): T[];
    static deserializeArray<T extends Model>(this: ( new() => T), first: ( new() => T ) | {}[], second?: {}[]): T[] {
        const ctor = typeof first === "function" ? first : this;
        const objects = typeof first === "function" ? second : first;
        return objects.map(object => Model.deserialize(ctor, object));
    }
}

这允许两种形式同时保留abstract

1 个答案:

答案 0 :(得分:0)

在我看来,没有必要同时拥有两者:

let myModel1 = MyModel.deserialize({});
let myModel2 = Model.deserialize(MyModel, {});

第一个表单足够且更易读,然后代码将如下所示:

type ModelCtor<T extends Model> = {
    new(): T;
    deserialize<T extends Model>(object: any): T;
    deserializeArray<T extends Model>(...objects: any[]): T[];
};

abstract class Model {
    static deserialize<T extends Model>(this: ModelCtor<T>, object: any): T {
        return new this();
    }

    static deserializeArray<T extends Model>(this: ModelCtor<T>, ...objects: any[]): T[] {
        return objects.map(object => this.deserialize(object));
    }
}

class MyModel extends Model { }

let myModel = MyModel.deserialize({});
let myModels = MyModel.deserializeArray({}, {}, {});

code in playground

但是,如果由于某种原因超出我的意愿,你确实希望同时拥有这两种形式,那么你可以这样做:

type ModelCtor<T extends Model> = {
    new(): T;
    deserialize<T extends Model>(object: any): T;
    deserialize<T extends Model>(ctor: ModelCtor<T>, object: any): T;

    deserializeArray<T extends Model>(...objects: any[]): T[];
    deserializeArray<T extends Model>(ctor: ModelCtor<T>, ...objects: any[]): T[];
};

class Model {
    static deserialize<T extends Model>(this: ModelCtor<T>, object: any): T;
    static deserialize<T extends Model, S extends Model>(this: ModelCtor<T>, ctor: ModelCtor<S>, object: any): S;
    static deserialize<T extends Model, S extends Model>(this: ModelCtor<T>, first: ModelCtor<S> | any, second?: any): S {
        return second === undefined ? new this() : new first();
    }

    static deserializeArray<T extends Model>(this: ModelCtor<T>, ...objects: any[]): T[];
    static deserializeArray<T extends Model, S extends Model>(this: ModelCtor<T>, ctor: ModelCtor<S>, ...objects: any[]): S[];
    static deserializeArray<T extends Model, S extends Model>(this: ModelCtor<T>, ...objects: any[]): S[] {
        const ctor: ModelCtor<S> = typeof objects[0] === "function" ? objects[0] : this as any;
        return objects.map(object => ctor.deserialize(ctor, object));
    }
}

class MyModel extends Model { }

let myModel1 = MyModel.deserialize({});
let myModels1 = MyModel.deserializeArray({}, {}, {});

let myModel2 = Model.deserialize(MyModel, {});
let myModels2 = Model.deserializeArray(MyModel, {}, {}, {});

code in playground

代码更加完整(我认为没有充分的理由) 另外,我必须从abstract删除Model部分,否则会出现此错误:

  

&#39; this&#39;类型&#39;类型的上下文&#39;不能分配方法&#39;这个&#39;类型&#39; ModelCtor&#39;   无法将抽象构造函数类型分配给非抽象构造函数类型。

对于这两行:

let myModel2 = Model.deserialize(MyModel, {});
let myModels2 = Model.deserializeArray(MyModel, {}, {}, {});

也许您可以更多地使用它并在不删除abstract的情况下修复这些错误。