gpt4 book ai didi

angular - typescript 中的 CollectionResult

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:00 25 4
gpt4 key购买 nike

我正在尝试为从服务器获取响应的代理服务创建一个通用类。当我留下来实现这个类时,我遇到了一个问题,从泛型类调用一个方法。如何从 Js(item) 调用以泛型类型声明的静态方法?

export class CollectionResult<T> implements ICollectionResult<T> {
items: Array<T>;

constructor(data?: T) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
}

init(data?: any) {
if (data) {
Object.assign(this, data);
this.items = [];
for (let item of data.items) {
this.items.push(T//NOTE: Error here//.fromJS(item));
}
}
}

fromJS(data: any): CollectionResult<T> {
let result = new CollectionResult<T>();
result.init(data);
return result;
}

toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
Object.assign(data, this);
if (this.items && this.items.constructor === Array) {
data["items"] = [];
for (let item of this.items) {
data["items"].push(item);
}
}
return data;
}
}

export interface ICollectionResult<T> {
items: Array<T>;
}

//传递 CollectionResult 的通用类

export class ProgramDto implements 
IProgramDto {
year: number;
month: number;
accounts: number;

constructor(data?: ProgramDto) {
if (data) {
for (var property in data) {
if (data.hasOwnProperty(property))
(<any>this)[property] = (<any>data)[property];
}
}
}

init(data?: any) {
if (data) {
Object.assign(this, data);
}
}

static fromJS(data: any): ProgramDto {
let result = new ProgramDto();
result.init(data);
return result;
}

toJSON(data?: any) {
data = typeof data === 'object' ? data : {};
Object.assign(data, this);

return data;
}
}

export interface IProgramDto {
year: number;
month: number;
accounts: number;
}

//代理方法中的用法

Get(): Observable<CollectionResult<ProgramDto >> {
let url = `${this.baseUrl}/Api/Controller/Method`;
url = url.replace(/[?&]$/, "");
let options = this.getOptions();
return this.sendRequestWithResponse(url, options, CollectionResult<ProgramDto>.fromJS, new CollectionResult<ProgramDto>());
}

最佳答案

您不能访问泛型类型参数的成员。类型参数在编译期间被删除,因此除了类型检查之外,您不能依赖它们做任何其他事情。

您可以将类作为参数传入并限制它具有 fromJs 方法:

type CollectionResultDataClass<T> = {
new(...a: any[]): T
fromJs(o: any): T
}

export class CollectionResult<T> implements ICollectionResult<T> {
items: Array<T>;

constructor(private cls: CollectionResultDataClass<T>, data?: T) {
// ...
}

init(data?: any) {
if (data) {
Object.assign(this, data);
this.items = [];
for (let item of data.items) {
this.items.push(this.cls.fromJs(item));
}
}
}

fromJS(data: any): CollectionResult<T> {
let result = new CollectionResult<T>(this.cls);
result.init(data);
return result;
}

toJSON(data?: any) {
//..
}
}

//Usage
class MyDataClass {
static fromJs(o: any) {
return Object.assign(new MyDataClass(), o);
}
}

new CollectionResult(MyDataClass, new MyDataClass());

关于angular - typescript 中的 CollectionResult<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56327217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com