realfifi
realfifi

Reputation: 29

Property does not exist on type 'object' with Svelte (using TypeScript)

I'm trying to pass objects from the array to components.

Screenshot 1

Like this: Example

But it says that property title doesn't exist in this type. Example 2

Also, the console log acts weirdly. What am I doing wrong?

Upvotes: 2

Views: 5211

Answers (1)

brunnerh
brunnerh

Reputation: 185445

You declare the property to be an object which specifies just about nothing, so of course the compiler will not know that there is a title. You have to be more specific (or tell the compiler to accept anything using any which is not recommended).

You can define a named interface or define the type inline:

export let data: Data;

interface Data { title: string, desc: string, img: string, prize: number }
export let data: { title: string, desc: string, img: string, prize: number };

(Btw, prize should probably be price.)

Upvotes: 3

Related Questions