简体   繁体   English

打字稿 await toPromise 类型转换错误

[英]Typescript await toPromise type casting error

I have following problem:我有以下问题:

export class FloorManagerComponent implements OnInit
{
    public meta = {
        list: [],
        building: Building,
        loading: true,
    };

    constructor(
        private router: Router, 
        private activatedRoute: ActivatedRoute, 
        private buildingService: BuildingService
    )
    {;}

    public async ngOnInit() 
    {
        let params = await this.activatedRoute.params.first().toPromise();

        this.meta.building = await this.buildingService // line 42: ERROR
            .getBuilding(params['buildingId'])          // line 42: ERROR
            .toPromise();                               // line 42: ERROR
    }

    ...
}

I get following error during compilation:我在编译过程中遇到以下错误:

[at-loader] ./src/pages/floor/manager/floorManager.component.ts:42:9 TS2322: Type 'Building' is not assignable to type 'typeof Building'. [at-loader] ./src/pages/floor/manager/floorManager.component.ts:42:9 TS2322:类型“Building”不可分配给类型“typeof Building”。 Property 'prototype' is missing in type 'Building'. “建筑”类型中缺少属性“原型”。

Im stuck here - any idea?我被困在这里 - 知道吗?

Here are body of used classes:以下是使用的类的主体:

export class Building {

    constructor(
        public id: number,
        public name: string, 
        public image_url: string, 
    ) {}

    ...
}


export class BuildingService {

    public getBuilding(buildingId: number)
    {
        return this.http.get(Url.api('buildings/' + buildingId))
            .map( (response) => { 
                return Obj.cast(response, Building);                  
            } );
    }

    ...

}

export class Obj {

    /**
     * This method allow to cast json (and not only) obiects to given type including methods
     * CAUTION: Constructor of 'type' T of object obj is never call during casting
     * Example usage:
     *
     * let space: Space = this.cast(spaceFromJson,Space);
     *
     * (we use type: { new(...args): T} tp creeate fat arrow functions in prototpye...  https://stackoverflow.com/a/32186367/860099)
     *
     * @param obj object (from json) that has only fields and no methods
     * @param type desired type
     * @returns {any} object that hava fields from obj and methods from type
     */
    public static cast<T>(obj, type: { new(...args): T} ): T 
    {
        obj.__proto__ = type.prototype;
        return obj;
    }
    ...
}

I found solution:我找到了解决方案:

public meta = {
    list: [],
    building: null as Building,
    loading: true,
};

So we should change building: Building to building: null as Building .所以我们应该将building: Building改为building: null as Building Because we use "object notation" in meta field definition the mistake was that I use "non-object notation" on subfield building (in "object notation" the : change meaning from type to value definition).因为我们在meta字段定义中使用“对象符号”,错误是我在子字段building使用了“非对象符号”(在“对象符号”中:将含义从类型更改为值定义)。

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

相关问题 async await + toPromise挂起 - async await + toPromise hangs 在Typescript中进行类型转换 - Type casting in Typescript StackBlitz 中的错误:“意外的严格模式保留字”试图将 async/await 与 subscription.toPromise() 一起使用 - Error in StackBlitz: 'Unexpected strict mode reserved word' trying to use async/await with subscription.toPromise() 输入toPromise和WebStorm时出错 - Error on typings for toPromise and WebStorm Jasmine 带有 toPromise 的 Cold Marble - 永无止境的等待 - Jasmine Cold Marble with toPromise - never ending await Angular 2 / Ionic 2 中的 Typescript Casting 错误 - Typescript Casting error in Angular 2 / Ionic 2 Angular/ TypeScript - 如何在 HTML 文件中键入铸件? - Angular/ TypeScript - How to Type casting in HTML file? Angular报错“toPromise is not a function”有解决办法吗? - Is there a solution to the error "toPromise is not a function" in Angular? 为什么我在Angular应用程序中遇到此错误? 类型“可观察”不存在属性“ toPromise” <Response> “ - Why I am obtaining this error in an Angular application? Property 'toPromise' does not exist on type 'Observable<Response>' 类型'Observable <Response>'上不存在属性'toPromise' - Property 'toPromise' does not exist on type 'Observable<Response>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM