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

Add const to exact comparisons #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rosettaroberts-impact
Copy link

This improves postgres enum support by making as const not needed in conditions when creating conditions for columns with enum types.

This typescript feature requires typescript 5.0. This might be problematic because the peer dependency is "typescript": ">=4.1".

This is an example of the problem this PR is trying to fix.

Schema
declare module 'zapatos/schema' {
  export type enum_profile_avail_check_status = 'Avail' | 'Unavail' | 'Waiting';

  export namespace profile_avail_check {
    export type Table = 'profile_avail_check';
    export interface Whereable {
      /**
       * **profile_avail_check.status**
       * - `enum_profile_avail_check_status` in database
       * - `NOT NULL`, default: `'Waiting'::enum_profile_avail_check_status`
       */
      status?:
        | enum_profile_avail_check_status
        | db.Parameter<enum_profile_avail_check_status>
        | db.SQLFragment
        | db.ParentColumn
        | db.SQLFragment<
            any,
            | enum_profile_avail_check_status
            | db.Parameter<enum_profile_avail_check_status>
            | db.SQLFragment
            | db.ParentColumn
          >;
    }
  }
}

Before this change, the following code has a typescript error:

db.select('profile_avail_check', {
  status: db.conditions.ne('Waiting'),
});

The reason it has a typescript error is because the type of db.conditions.ne('Waiting') is inferred to be SQLFragment<boolean | null, string>, which is not a subtype of db.SQLFragment<any, enum_profile_avail_check_status>. A workaround is to instead do

db.select('profile_avail_check', {
  status: db.conditions.ne('Waiting' as const),
});

With the workaround, db.conditions.ne('Waiting') is inferred to be SQLFragment<boolean | null, 'Waiting'> which is a subtype of db.SQLFragment<any, enum_profile_avail_check_status>. However, it can be annoying to remember this workaround.

This PR removes the need for the workaround by telling typescript to infer the narrowest type possible for the parameter.

This improves enum support by allowing making `as const` not needed in conditions when creating conditions for columns with enum types.

This typescript feature requires typescript 5.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant