drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Nullable type inference with .mapWith()

Open
#571 0 comments 3 reactions 0 assignees View on GitHub
bug bug/fixed-in-beta has-pr priority qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Describe want to want

### Description
When using `.mapWith()`, nullable type inference is not being properly handled. Without the `.mapWith()`, the type is inferred correctly, but when it's mapped, the type is always non-nullable. This seems to be a niche situation, but it would be helpful to address this edge case.

### Steps to reproduce
Here's the code snippet that demonstrates the issue:

```
someNewField: sql`CASE WHEN some_field IS NOT NULL THEN some_field END`.mapWith(table.someField),
```

`Type (property) destination: SQL`

But if the `.mapWith()` is removed, it is correctly inferred to:

`(property) destination: SQL`

### Expected behavior
The nullable type should be inferred correctly when using `.mapWith()`

As per @dankochetov solution, there is a workaround

### Workaround
A custom mapper function can be used as a temporary solution:
```
function mapper(value: any): Type | null {
return table.someField.mapFromDriverValue(value);
}

sql``.mapWith(mapper)
```

### Environment
Drizzle version: `0.25.4`
TypeScript version: `5.0.3`
Node.js version: `18`

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.