K
Kysely12mo ago
Lion

TS2345: Argument of type  "id"  is not assignable to parameter of type  ReferenceExpression<Database

Code:
const newToken = await db
.selectFrom("tokens")
.selectAll()
.where("id", "=", token.insertId)
.$narrowType<Token>()
.executeTakeFirst();
const newToken = await db
.selectFrom("tokens")
.selectAll()
.where("id", "=", token.insertId)
.$narrowType<Token>()
.executeTakeFirst();
Schema File:
import { ColumnType, Generated, Selectable } from "kysely";
import { TokenType } from "./lib/token";

export interface Database {
tokens: TokenType;
}

export interface TokenTable {
id: Generated<Number>;

secret: string;
description: string;
type: TokenType;

created_at: ColumnType<Date, string | undefined, never>;
}

export type Token = Selectable<TokenTable>;
import { ColumnType, Generated, Selectable } from "kysely";
import { TokenType } from "./lib/token";

export interface Database {
tokens: TokenType;
}

export interface TokenTable {
id: Generated<Number>;

secret: string;
description: string;
type: TokenType;

created_at: ColumnType<Date, string | undefined, never>;
}

export type Token = Selectable<TokenTable>;
TokenType Enum (incase it matters):
export enum TokenType {
ADMIN,
SERVER,
CLIENT,
}
export enum TokenType {
ADMIN,
SERVER,
CLIENT,
}
The code is giving me a typescript error for the first argument of the where clause, "id":
TS2345: Argument of type  "id"  is not assignable to parameter of type  ReferenceExpression<Database, "tokens"> 
TS2345: Argument of type  "id"  is not assignable to parameter of type  ReferenceExpression<Database, "tokens"> 
Solution:
So all in all ```ts import { ColumnType, Generated, Selectable } from "kysely"; import { TokenType } from "./lib/token";...
Jump to solution
5 Replies
Lion
LionOP12mo ago
I also tried "tokens.id", same ts error :(
Igal (mobile)
Igal (mobile)12mo ago
Hey 👋 You used TokenType instead of TokenTable in Database.
koskimas
koskimas12mo ago
Also id uses Number instead of number. That's the "class" version of number that should never be used as a type Also insertId is a bigint. You need to cast it to a normal number
Solution
koskimas
koskimas12mo ago
So all in all
import { ColumnType, Generated, Selectable } from "kysely";
import { TokenType } from "./lib/token";

export interface Database {
tokens: TokenTable; // <-- Uses the correct type
}

export interface TokenTable {
id: Generated<number>; // <-- number instead of Number

secret: string;
description: string;
type: TokenType;

created_at: ColumnType<Date, string | undefined, never>;
}

export type Token = Selectable<TokenTable>;

const newToken = await db
.selectFrom("tokens")
.selectAll()
// Cast bigint to number
.where("id", "=", Number(token.insertId))
// .$narrowType<Token>() <-- No need for this anymore.
.executeTakeFirst();
import { ColumnType, Generated, Selectable } from "kysely";
import { TokenType } from "./lib/token";

export interface Database {
tokens: TokenTable; // <-- Uses the correct type
}

export interface TokenTable {
id: Generated<number>; // <-- number instead of Number

secret: string;
description: string;
type: TokenType;

created_at: ColumnType<Date, string | undefined, never>;
}

export type Token = Selectable<TokenTable>;

const newToken = await db
.selectFrom("tokens")
.selectAll()
// Cast bigint to number
.where("id", "=", Number(token.insertId))
// .$narrowType<Token>() <-- No need for this anymore.
.executeTakeFirst();
Lion
LionOP12mo ago
Thanks, that fixed everything :)
Want results from more Discord servers?
Add your server