Angular 4 Property does not exist for type Object on build

I am creating a project using Angular, I started the project using angular-cli, and when I try to start ng build --prod, I keep getting this error:

The 'description' property does not exist for type Object

The code generating this error is as follows:

export class AppComponent {
    product: Object = {};

    constructor(
        private store: StoreService,
        private request: RequestService,
    ) {
        this.product = this.request.getProduct(_id);
    }
}

<p>{{product.description}}</p>

I read some content about this, and the error is that I use a type definition to define a product as an object, but I do not pass any property definition.

I know that I can define the interface, as I do with arrays, but I could not do this. I donโ€™t know if I am defining this incorrectly, this is how I tried:

export interface ProductInterface {
    id: Number;
    description: String;
    title: String;
}

product: Object<ProductInterface> = {};

But it also gives me errors. What do I need to do to avoid this?

+23
6

. html , ( Object)

.

product: ProductInterface = {};

. , ,

product: ProductInterface;

, , Object < >

+14

, product: ProductInterface;, .

, , {{ product?. description }}

+3

OnInit, , OnInit

ngOnInit(){
    this.product = this.request.getProduct(_id); // who is _id
} 

, getProduct() http-,

this.request.getProduct(_id).subscribe((data) => {
   this.product=data;
});
0

,

,

export interface ProductInterface {
    id: Number;
    description: String;
    title: String;
}

export interface ProductInterface {
    public id: Number;
    public description: String;
    public title: String;
}
0

any Object Object , , . , :

export class AppComponent {
    product: any;

    constructor(
        private store: StoreService,
        private request: RequestService,
    ) {
        this.product = this.request.getProduct(_id);
    }
}
0

..

ngOnInit(){
    this.product = this.request.getProduct(_id); // who is _id
} 

: .

this.request.getProduct(_id).subscribe((data: any) => {
   this.product=data;
});

, -. ( / .)

0
source

All Articles