Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 Property does not exist on type Object on build

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?

like image 919
celsomtrindade Avatar asked Jun 28 '17 19:06

celsomtrindade


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

Does not exist in type TypeScript?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string.

Does not exist on type unknown TS 2339?

To fix the error "TS2339: Property 'x' does not exist on type 'Y'" with TypeScript, we should make sure the properties are listed in the interface that's set as the type of the object. interface Images { main: string; [key: string]: string; } const getMainImageUrl = (images: Images): string => { return images. main; };

Has no initializer and is not definitely assigned in the constructor angular?

The error "Property has no initializer and is not definitely assigned in the constructor" occurs when we declare a class property without initializing it. To solve the error, provide an initial value for the class property, e.g. name: string = 'James'; or use a non-null assertion.


1 Answers

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

like image 67
LLai Avatar answered Sep 27 '22 16:09

LLai