Confused about the return type of this function

So i have a function which fetches the user. My goal is to return user if exists else return null. However in order to achieve this i need to manually check the length of the response and if its zero i return null. Overall my code looks so much duplicated and verbose. I tried using this but this doesnt shows null as return type
No description
3 Replies
rphlmr ⚡
rphlmr ⚡2mo ago
You can try this:
async function getUser(name: string) {
const results = await db.select().from(users).where(eq(users.name, name));
return results.at(0) || null;
}
async function getUser(name: string) {
const results = await db.select().from(users).where(eq(users.name, name));
return results.at(0) || null;
}
No description
rphlmr ⚡
rphlmr ⚡2mo ago
Your initial code is not wrong; however, from the type system’s point of view, ⁠data can’t be undefined/null (that is wrong, it can).
ShastRi
ShastRi2mo ago
thanks
Want results from more Discord servers?
Add your server