Back

[SOLVED] Custom types for document attributes

  • 0
  • Databases
  • Self Hosted
andrei
12 Aug, 2023, 09:53

I am trying to get my types sorted out with the custom collection schema. I am trying to add my custom properties to Models.Document like this:

TypeScript
interface Picture extends Models.Document {
  user_id: string,
  name: string,
  pic_id: string,
  an: number,
  descriere: string
}

And trying to use it like this:

TypeScript
db.listDocuments(..).documents.map((a: Picture) => {});

Except, it's not compatible: ts Argument of type '(a: Picture) => { user_id: string; name: string; pic_id: string; an: number; descriere: string; }' is not assignable to parameter of type '(value: Document, index: number, array: Document[]) => { user_id: string; name: string; pic_id: string; an: number; descriere: string; }'.ts(2345)

What would be the solution?

TL;DR
The user was trying to add custom properties to the `Models.Document` type in TypeScript. They defined an interface `Picture` extending `Models.Document` with the custom properties but encountered an error when trying to use it. The error indicated that the argument type was not assignable. The solution is to use a type assertion to force the type compatibility. An example code sample was provided.
XaFigg
12 Aug, 2023, 10:09

I think you need to force the assertion.

XaFigg
12 Aug, 2023, 10:09
TypeScript
type Picture = Models.Document & {
    user_id: string,
    name: string,
    pic_id: string,
    an: number,
    descriere: string
}
XaFigg
12 Aug, 2023, 10:10

Basically it's the same as you but with a "type"

andrei
12 Aug, 2023, 10:54

fixed it

andrei
12 Aug, 2023, 10:54

thanks!

XaFigg
12 Aug, 2023, 10:57

πŸ‘Œ

Drake
12 Aug, 2023, 15:10

[SOLVED] Custom types for document attributes

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more