6

我有以下服务

@Injectable()
export class CollectionService<T> {    

    constructor(protected http: Http) {}

    factory<T>(item?: any): T {
        let type: new (item?: any) => T;
        return new type(item);
    }

    item(id): Observable<T> {
        return this.http.get(`${this.baseUrl}/${id}`)
            .map((resp: Response)=> this.factory(resp.json()))
            .catch((error: any) => {
                return Observable.throw(error);
            });
    }
}

编译成功通过,但我在浏览器控制台中出现以下错误

TypeError: type is not a constructor
at PortalVideoService.webpackJsonp.../../../../../src/app/services/collection/collection.service.ts.CollectionService.factory (http://localhost:4200/main.bundle.js:1568:16)
at http://localhost:4200/main.bundle.js:1580:64
at Array.map (native)
at MapSubscriber.project (http://localhost:4200/main.bundle.js:1580:29)
at MapSubscriber.webpackJsonp.../../../../rxjs/operator/map.js.MapSubscriber._next (http://localhost:4200/vendor.bundle.js:24034:35)
at MapSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (http://localhost:4200/vendor.bundle.js:13455:18)
at XMLHttpRequest.onLoad (http://localhost:4200/vendor.bundle.js:101703:38)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.bundle.js:2838:31)
at Object.onInvokeTask (http://localhost:4200/vendor.bundle.js:93420:37)
at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.bundle.js:2837:36)

如何创建 T 类型的对象?

4

3 回答 3

7

该名称T仅在编译时存在。您可以将其用于类型检查,但您不能构造 T 类型的对象,除非您有权访问构造函数。

更改 的定义factory以将运行时类型构造函数作为参数:

factory<T>(type: {new(): T}, item?: any): T {
    return new type(item);
}

现在唯一的问题是弄清楚你从哪里得到类型参数;您可能还需要将它传递给item()方法,以便编译器知道要生成什么类型​​的Observable<T>.

于 2017-07-10T12:19:02.590 回答
0

您可以将构造函数作为类泛型而不是方法传递。

export class CollectionService<T, CT extends { new(item?: any): T }> {    

    constructor(protected http: Http, private type: CT) {}

    factory(item?: any): T {
        return new this.type(item);
    }

    // ...
}

class CustomClass {
    constructor(private item?: any) {
    }
 };


let cs = new CollectionService<CustomClass, { new(): CustomClass }>(http, CustomClass);
console.dir(cs.factory(123));

// UPDATE
class CustomClassService extends CollectionService<CustomClass, { new(): CustomClass }> {
    constructor(protected http: Http) {
        super(http, CustomClass);
    }
}

let ccs = new CustomClassService(http);
console.dir(ccs.factory(456));
于 2017-07-10T12:51:19.993 回答
0

没有必要做你正在做的事情,只需做

.map((resp: Response)=> resp.json())

如果您想要响应的自定义类型,请执行

.map((resp: Response)=> resp.json() as CustomResponse) 

接口在哪里CustomResponse

于 2017-07-10T11:38:16.840 回答