sqlx-ts icon indicating copy to clipboard operation
sqlx-ts copied to clipboard

Suggestions for improving type generation for JSON fields and replacing any with unknown

Open Leo5878 opened this issue 2 months ago • 0 comments

Hi! I noticed that in the latest sqlx-ts update, it no longer crashes when encountering JSON fields - thank you so much for that improvement.

I have a couple of suggestions related to type generation, and I might even be able to help implement them.

In my Postgres database, I have a JSON column, and sqlx-ts currently generates an interface like this:

export interface IConfigsUserResult {
  config: object | null;
  config_id: string | null;
  id: number;
}

What if instead of object, we used a generic type? This would allow developers to describe the shape of the JSON stored in the database and use it as the return type:

export interface IConfigsUserResult<T = object> {
  config: T | null;
  config_id: string | null;
  id: number;
}

I understand that this approach mixes layers and that sqlx-ts cannot guarantee the JSON structure, but this responsibility could be left to the developer. If they don’t want to provide a specific type, they can simply omit the generic, and object would be used by default.

Replace types any with unknown

My second suggestion is to replace any with unknown. This would enforce stricter typing and encourage developers to properly validate values returned from the database in cases where sqlx-ts cannot infer a precise type.

Leo5878 avatar Nov 10 '25 00:11 Leo5878