Angular 类型';未知';不可分配给类型';T';

Angular 类型';未知';不可分配给类型';T';,angular,Angular,我正在从Angular 5迁移到Angular 8应用程序,遇到以下错误。不确定是什么问题 错误 Type 'unknown' is not assignable to type 'T'. 'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'. 代码 public getClientComp

我正在从Angular 5迁移到Angular 8应用程序,遇到以下错误。不确定是什么问题

错误

Type 'unknown' is not assignable to type 'T'.  'unknown' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
代码

public getClientCompanyAccountsEndpoint(clientCompanyId:number):可观察{
const url=this.\u baseUrl+this.\u clientCompanyAccountsUrl+clientCompanyId;
返回this.http.get(url,this.getRequestHeaders())
.pipe(catchError)(错误=>{
返回此.handleError(错误,()=>this.getClientCompanyAccountsEndpoint(clientCompanyId));
}));
}
在深入研究代码之后,我可以看到已经定义了几个服务和端点类。我仍然不明白为什么要使用泛型,因为每个端点类都有自己的getClientCompanyAccountsEndpoint方法

服务等级

@Injectable()
export class AddAccountService {

    constructor(private _addAccountEndpoint: AddAccountEndpoint){}

    getClientCompanyAccounts(clientCompanyId: number){
        return this._addAccountEndpoint.getClientCompanyAccountsEndpoint<AccountModel[]>(clientCompanyId);
    }
}
        @Injectable()
    export class ClientEndpoint extends EndpointFactory {
        private readonly _baseUrl: string = this.configurations.baseUrl;
            public getClientCompanyAccountsEndpoint<T>(clientCompanyId: number): Observable<T> {
            return this.http.get<T>(this._clientCompanyAccountsUrl + clientCompanyId, this.getRequestHeaders())
                .pipe(catchError(error => {
                    return this.handleError(error, () => this.getClientCompanyAccountsEndpoint(clientCompanyId));
                }));
        }
        }
@Injectable()
导出类AddAccountService{
构造函数(私有_addAccountEndpoint:addAccountEndpoint){}
getClientCompanyAccounts(clientCompanyId:编号){
返回此值。\u addAccountEndpoint.getClientCompanyAccountsEndpoint(clientCompanyId);
}
}
端点类

    @Injectable()
    export class AddAccountEndpoint extends EndpointFactory {
        private readonly _baseUrl: string = this.configurations.baseUrl;
        private readonly _clientCompanyAccountsUrl = '/api/client-company-accounts/';

        constructor(http: HttpClient, protected configurations: ConfigurationService, injector: Injector) {
            super(http, configurations, injector);
        }

        public getClientCompanyAccountsEndpoint<T>(clientCompanyId: number): Observable<T> {
            const url = this._baseUrl + this._clientCompanyAccountsUrl + clientCompanyId;
            return this.http.get<T>(url, this.getRequestHeaders())
            .pipe(catchError(error => {
                return this.handleError(error, () => this.getClientCompanyAccountsEndpoint(clientCompanyId));
            }));
        }
    }
        @Injectable()
    export class ClientService {

        constructor(private router: Router, private clientEndpoint: ClientEndpoint) {

        }

            getClientCompanyAccounts(clientCompanyId: number) {
               return this.clientEndpoint.getClientCompanyAccountsEndpoint<SettlementAccountModel[]>(clientCompanyId);
            }
        }
@Injectable()
导出类AddAccountEndpoint扩展EndpointFactory{
private readonly_baseUrl:string=this.configurations.baseUrl;
私有只读_clientCompanyAccountsUrl='/api/client company accounts/';
构造函数(http:HttpClient,受保护配置:ConfigurationService,injector:injector){
超级(http、配置、注入器);
}
public getClientCompanyAccountsEndpoint(clientCompanyId:number):可观察{
const url=this.\u baseUrl+this.\u clientCompanyAccountsUrl+clientCompanyId;
返回this.http.get(url,this.getRequestHeaders())
.pipe(catchError)(错误=>{
返回此.handleError(错误,()=>this.getClientCompanyAccountsEndpoint(clientCompanyId));
}));
}
}
服务等级

    @Injectable()
    export class AddAccountEndpoint extends EndpointFactory {
        private readonly _baseUrl: string = this.configurations.baseUrl;
        private readonly _clientCompanyAccountsUrl = '/api/client-company-accounts/';

        constructor(http: HttpClient, protected configurations: ConfigurationService, injector: Injector) {
            super(http, configurations, injector);
        }

        public getClientCompanyAccountsEndpoint<T>(clientCompanyId: number): Observable<T> {
            const url = this._baseUrl + this._clientCompanyAccountsUrl + clientCompanyId;
            return this.http.get<T>(url, this.getRequestHeaders())
            .pipe(catchError(error => {
                return this.handleError(error, () => this.getClientCompanyAccountsEndpoint(clientCompanyId));
            }));
        }
    }
        @Injectable()
    export class ClientService {

        constructor(private router: Router, private clientEndpoint: ClientEndpoint) {

        }

            getClientCompanyAccounts(clientCompanyId: number) {
               return this.clientEndpoint.getClientCompanyAccountsEndpoint<SettlementAccountModel[]>(clientCompanyId);
            }
        }
@Injectable()
导出类客户端服务{
构造函数(专用路由器:路由器,专用客户端端点:客户端端点){
}
getClientCompanyAccounts(clientCompanyId:编号){
返回此.clientEndpoint.getClientCompanyAccountsEndpoint(clientCompanyId);
}
}
端点类

@Injectable()
export class AddAccountService {

    constructor(private _addAccountEndpoint: AddAccountEndpoint){}

    getClientCompanyAccounts(clientCompanyId: number){
        return this._addAccountEndpoint.getClientCompanyAccountsEndpoint<AccountModel[]>(clientCompanyId);
    }
}
        @Injectable()
    export class ClientEndpoint extends EndpointFactory {
        private readonly _baseUrl: string = this.configurations.baseUrl;
            public getClientCompanyAccountsEndpoint<T>(clientCompanyId: number): Observable<T> {
            return this.http.get<T>(this._clientCompanyAccountsUrl + clientCompanyId, this.getRequestHeaders())
                .pipe(catchError(error => {
                    return this.handleError(error, () => this.getClientCompanyAccountsEndpoint(clientCompanyId));
                }));
        }
        }
@Injectable()
导出类ClientEndpoint扩展EndpointFactory{
private readonly_baseUrl:string=this.configurations.baseUrl;
public getClientCompanyAccountsEndpoint(clientCompanyId:number):可观察{
返回this.http.get(this.\u clientCompanyAccountsUrl+clientCompanyId,this.getRequestHeaders())
.pipe(catchError)(错误=>{
返回此.handleError(错误,()=>this.getClientCompanyAccountsEndpoint(clientCompanyId));
}));
}
}
我是否需要分别这样编写端点和服务类

 getClientCompanyAccounts(clientCompanyId: number) {
        return this.clientEndpoint.getClientCompanyAccountsEndpoint(clientCompanyId);
    }

    public getClientCompanyAccountsEndpoint(clientCompanyId: number): Observable<AccountModel[]> {
        const url = this._baseUrl + this._clientCompanyAccountsUrl + clientCompanyId;
        return this.http.get<AccountModel[]>(url, this.getRequestHeaders())
        .pipe(catchError(error => {
            return this.handleError(error, () => this.getClientCompanyAccountsEndpoint(clientCompanyId));
        }));
    }

 getClientCompanyAccounts(clientCompanyId: number){
        return this._addAccountEndpoint.getClientCompanyAccountsEndpoint(clientCompanyId);
    }

public getClientCompanyAccountsEndpoint(clientCompanyId: number): Observable<SettlementAccountModel[]> {
        return this.http.get<SettlementAccountModel[]>(this._clientCompanyAccountsUrl + clientCompanyId, this.getRequestHeaders())
            .pipe(catchError(error => {
                return this.handleError(error, () => this.getClientCompanyAccountsEndpoint(clientCompanyId));
            }));
    }
getClientCompanyAccounts(clientCompanyId:number){
返回此.clientEndpoint.getClientCompanyAccountsEndpoint(clientCompanyId);
}
public getClientCompanyAccountsEndpoint(clientCompanyId:number):可观察{
const url=this.\u baseUrl+this.\u clientCompanyAccountsUrl+clientCompanyId;
返回this.http.get(url,this.getRequestHeaders())
.pipe(catchError)(错误=>{
返回此.handleError(错误,()=>this.getClientCompanyAccountsEndpoint(clientCompanyId));
}));
}
getClientCompanyAccounts(clientCompanyId:编号){
返回此值。\u addAccountEndpoint.getClientCompanyAccountsEndpoint(clientCompanyId);
}
public getClientCompanyAccountsEndpoint(clientCompanyId:number):可观察{
返回this.http.get(this.\u clientCompanyAccountsUrl+clientCompanyId,this.getRequestHeaders())
.pipe(catchError)(错误=>{
返回此.handleError(错误,()=>this.getClientCompanyAccountsEndpoint(clientCompanyId));
}));
}

我建议捕获以下错误

public getClientCompanyAccountsEndpoint<T>(clientCompanyId: number): Observable<T> {
    const url = this._baseUrl + this._clientCompanyAccountsUrl + clientCompanyId;

    return this.http.get<T>(url, this.getRequestHeaders())
    .pipe(tap(_ => {}, err => this.handleError(error, () => this.getClientCompanyAccountsEndpoint(clientCompanyId))));
}
Tap运算符定义为

tap({
  next: val => {
    // on next 11, etc.
    console.log('on next', val);
  },
  error: error => {
    console.log('on error', error.message);
  },
  complete: () => console.log('on complete')
})

为什么这个方法是通用的?服务器如何返回取决于您选择的方法调用类型的内容?特别是当您在不指定任何泛型类型的情况下调用它时。nackend返回类似于
ClientCompanyAccounts
,对吗?因此,删除泛型类型T,并使用
this.http.get
。并将返回类型更改为
可观察的
。我自己也在努力理解这一点。它的旧代码,我正在尝试升级。我所做的就是使用pipeable运算符和catchError运算符。最初只有CatchError必须返回一个可观察的,所以我认为这个错误是由于handleError方法的类型变异造成的——它必须返回相同的类型。我刚刚更新了帖子,以显示应用程序中如何使用端点和服务类。是否只是用any替换
T
。。