unionAll nullable typing error

I have something like this
const blackPlayerOneEloChanges = db
.select({
player_id: matches.blackPlayerOne,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerOne));
const blackPlayerTwoEloChanges = db
.select({
player_id: matches.blackPlayerTwo,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerTwo));;
const blackPlayerOneEloChanges = db
.select({
player_id: matches.blackPlayerOne,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerOne));
const blackPlayerTwoEloChanges = db
.select({
player_id: matches.blackPlayerTwo,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerTwo));;
then I do a unionAll(blackPlayerOneEloChanges, blackPlayerTwoEloChanges and get the following error, because blackPlayerTwo is nullable, it's optional, the question is if there is anyway I can override the type of playerid in the blackPlayerTwoEloChanges query, since I'm only selecting the ones where it is not null. `The types of '.result' are incompatible between these types. Type '{ player_id: string | null; elo_change: number; }[]' is not assignable to type { player_id: string; elo_change: number; }[]'.`
Solution:
You could do this: ``ts const blackPlayerTwoEloChanges = db .select({ player_id: sql<string>${matches.blackPlayerTwo}`,...
Jump to solution
2 Replies
Solution
Angelelz
Angelelz12mo ago
You could do this:
const blackPlayerTwoEloChanges = db
.select({
player_id: sql<string>`${matches.blackPlayerTwo}`,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerTwo));
const blackPlayerTwoEloChanges = db
.select({
player_id: sql<string>`${matches.blackPlayerTwo}`,
elo_change: matches.blackEloChange,
})
.from(matches)
.where(isNotNull(matches.blackPlayerTwo));
Faust(Thuen)
Faust(Thuen)OP12mo ago
Lovely, that just works and was surprisingly easy, thank you very much.
Want results from more Discord servers?
Add your server