Simplify ID Retrieval from Nullable Structure in TypeScript

I want to get the ID from this option if it exists, but when using getOrNull, we can have either null or {id:number}. I can't directly use Struct.get("id"), so I need to check if it's not null and then use this struct, which is getting complicated. What is the best way to get the ID from this if it exists and return null if it doesn't?

class GetOrgWithoutGroup extends Data.TaggedError("GetOrgWithoutGroup")<{
  msg: string;
}> {}

export const foo = Database.pipe(
  Effect.tryMapPromise({
    catch: (err: any) => new GetOrgWithoutGroup({ msg: err.message }),
    try: ({ db, organizationTable, eq, isNull }) =>
      db
        .select({ id: organizationTable.id })
        .from(organizationTable)
        .where(isNull(organizationTable.groupId)),
  }),
  Effect.map(RA.head),
  Effect.map(Option.getOrNull)
);
Was this page helpful?