37

I'm building a project using Angular, I started the project using angular-cli and when I try to run ng build --prod i keep getting this error:

Property 'description' does not exist on type Object

The code generating this error is the following:

export class AppComponent {
    product: Object = {};

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

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

I was reading some content about this and the error is because I'm using type definition to define product as Object, but I'm not passing any property definition.

I know I could define an Interface, like I do with arrays, but I wasn't able to do it. I don't know if I'm defining it wrong, 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?

5
  • The type of product should be ProductInterface, not Object<ProductInterface>. Commented Jun 28, 2017 at 20:00
  • who is getProduct Commented Jun 28, 2017 at 20:14
  • Try this: ng build --prod --aot false Commented Mar 23, 2018 at 10:13
  • 6
    Or this one maybe better: ng build --prod --aot false --build-optimizer false Commented Mar 23, 2018 at 10:22
  • 3
    use any ie product: any; Commented Nov 5, 2018 at 11:04

9 Answers 9

31

For your first example. In your html, you are saying product has the property description (which it does not on type Object)

In your second example. You are initially defining product as an empty object

product: ProductInterface = {};

Which is missing the required fields of the interface. So you can remove the initialization, leaving

product: ProductInterface;

Also as others have noted, you do not need the Object<> syntax

Sign up to request clarification or add additional context in comments.

4 Comments

I have a dynamic form and don't know any form control while compilation, at compilation time I get for ex:Property 'name' does not exist on type '{}'. any solutions for this type of forms?
@NikhilRadadiya If there are unknown properties in your object you can add dynamic properties product: {[key: string]: string} This will allow you to assign whatever key (type string) to the object with a value (type string). This is called an index signature (basarat.gitbooks.io/typescript/docs/types/index-signatures.html)
Thank you very much man, you saved many lives, that index structure is working like a charm
@NikhilRadadiya glad I could help!
9

From my case..

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

Just adding data:any at subscription works fine.

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

This would helpful while the response data having more key, value pairs. (So Its hard/Time consuming to create Interface.)

But always the best practise is to use Interfaces ;)

Comments

8

If the property is dynamic (not known at compile time), you can use

someObject['someProperty']

instead of

someObject.someProperty

1 Comment

it doesn't change anything in Angular v16
5

First of all I would simply use product: ProductInterface; and you don't even have to initialize it.

Then, probably this will fix your error {{ product?. description }}

2 Comments

What does it mean to use ? ?
@celsomtrindade that is the safe navigation operator (I believe its called elvis operator in other languages) concretepage.com/angular-2/… . Basically angular won't look for the description field until product is defined. Without it you may get an cannot read property description of undefined error.
0

You must define the request in OnInit method, your controller that implements OnInit interface and define a new method

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

Assuming that getProduct() is http request that return a observable

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

1 Comment

that's not the point, I just removed the unecessary code and forgot this part. The request function is working fine.
0

It is safe to use any rather than Object when data is requested from server, because we don't know what will be returned from server. So you don't need to typecheck ie:

export class AppComponent {
    product: any;

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

1 Comment

While this may be true at the time it was written, TypeScript has since included unknown to serve this very purpose
0

If you use command ng serve and build success.

May,you have the trouble with build configuration

maybe you need to modify the className.prod.ts like className.ts

my another answer has more detail:

Angular - How to fix 'property does not exist on type' error?

Comments

0

In Angular 16 none of the other solutions worked.

Luckily, the documentation is clear about this: you can use $any(...):

{{ $any(product).unknownProperty }}

Comments

-2

In my case it worked after set my properties as public

So, just change this

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

to this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.