Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Implement $nullable helper, similar to $nonNull. #1070

Open
virtuallyunknown opened this issue Jul 6, 2024 · 0 comments
Labels
api Related to library's API enhancement New feature or request typescript Related to Typescript

Comments

@virtuallyunknown
Copy link
Contributor

Greetings!

I think this can be useful for the following reasons:

  1. I speculate this is probably not too difficult to implement.
  2. A similar helper for $nonNull already exists, "where you know an expression can't be null, but Kysely is unable to infer it.".
  3. I see it having practical application for cases where Kysely infers the type incorrectly (example below)

A quick example where the problem is present, I posted this on the server as well: https://kyse.link/COF71

As proof of concept, I cloned the repository and tweaked aggregate-function-builder.ts to add the following to see if it's gonna work:

export class AggregateFunctionBuilder<DB, TB extends keyof DB, O = unknown> implements AliasableExpression<O> {
  // ...
  $nullable(): AggregateFunctionBuilder<DB, TB, O | null> {
    return new AggregateFunctionBuilder(this.#props)
  }
}

and as far as I can tell, it gets the job done, so that when you use .$nullable(), the value is being inferred as {}[] | null.

The $notNull helper is also present in expression-wrapper.ts, json-path-builder.ts and raw-builder.ts, so I assume a potential new helper would also need to be added there.

Anyway, don't want to make this too long, but let me know if this is a good idea or not, and have a great day.

@igalklebanov igalklebanov added enhancement New feature or request api Related to library's API typescript Related to Typescript labels Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API enhancement New feature or request typescript Related to Typescript
Projects
None yet
Development

No branches or pull requests

2 participants