select with limit of 1
Is there a cleaner way of selecting only one item with proper type safety than this?
8 Replies
well, you don't need to specify the type explicitly, other than that it's fine
When i don't specify the type it's only User but it can be undefined when the user with that id does not exist in the database. That's why i do that.
idk it feels a bit weird that the select returns an array with a limit of 1
set
"noUncheckedIndexedAccess": true
in tsconfigoh didn't know that exists
ohh that produces so much chaos in my codebase 😅
you could also add
?.
before [0]
so the type is User | undefined
I've been doing something like
Which seems to get the inference right but I don't know how typesafe this is.
Why not just use
.get()
?does
.get()
returns object
or array
?
nevermind. i tested it, it returns object | undefined
, thanks ❤️