drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Replace `never` type to `undefined` in query/disallow selects with columns from tables not included

Open
#5,532 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [x] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

When performing a query that selects columns from a table that was not included in the query (either using `from` or a `join`), drizzle annotates the type as `never`.

This can cause possible bugs when passing the result to a function that expects all properties to have values - `never` is a subtype of all types and TS will consider it a valid object to pass.

E.g.
```ts
function takesFooBar(value: { foo: number, bar: number }) {
// impl...
}

const queryResult = (await db.select({ foo: RepairTable.id, bar: CustomerTable.id })
.from(RepairTable)
.execute()
)[0]; // type is { foo: number, bar: never }

takesFooBar(queryResult)
```
TS, even on strict, does not complain about passing `queryResult` to `takesFooBar`, even though `queryResult.bar` is of type `never` and `takesFooBar` expects `value.bar` to be a number.

Unless additional validation is performed, which imo defeats the purpose of using TS, this is a bug waiting to happen.

A solution could be to instead replace `never` with `undefined`, or disallow selecting columns from tables that are not part of the query.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.