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?
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!
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.
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; };
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With