Index on optional nested properties
Is there a reason that on the schema:
with indices
I can do
But the nested one gets a type error?
4 Replies
It's because the playerOne object is optional in your schema. The error message is confusing. The "undefined" is playerOne when it is missing. You can't read a property from an object that doesn't exist.
Hmm pretty sure the types are just wrong here -- I'll work on a fix. In the meantime, a type cast to get around it
userId as any
or a // @ts-expect-error
should unblock you (and you can verify you get the results you expect)
The mental model I use for indexes is that on every document insertion / change, we look up the values for each field path listed in the index, and essentially create a tuple that we can sort by (the index key). So documents without playerOne
will have playerOne.userId
as undefined
Maybe I'm too spoiled by ?. but I expected both variants to resolve to
Id<"users"> | undefined
Yeah I thought you were spoiled too, but looks like you were right! Nice one 😁