Skip to content

fix(types): make WhereOptions more accurate - #14368

Merged
ephys merged 1 commit into
v6from
ephys/v6-backports
Apr 12, 2022
Merged

ephys merged 1 commit into
v6from
ephys/v6-backports

Conversation

@ephys

@ephys ephys commented Apr 12, 2022 •

Copy link
Copy Markdown
Member

Backport of PR #14022 to v6
I only backported the typing part, the rest was dependent on the TypeScript migration.

Some typing tests are disabled because TS < 4.4 has false negatives with symbols so some errors go unreported, but nothing new from what we had in our old typings, and previous false positives should be resolved

@ephys ephys added the typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. label Apr 12, 2022
@ephys ephys self-assigned this Apr 12, 2022
@ephys
ephys force-pushed the ephys/v6-backports branch from 184d261 to 98c2344 Compare April 12, 2022 08:16
@ephys ephys mentioned this pull request Apr 12, 2022
4 of 6 tasks
@ephys
ephys requested a review from a team April 12, 2022 08:29
@ephys
ephys force-pushed the ephys/v6-backports branch from 98c2344 to e2b9a5c Compare April 12, 2022 08:36
@ephys
ephys force-pushed the ephys/v6-backports branch from e2b9a5c to 6c1ad46 Compare April 12, 2022 08:38

@WikiRik WikiRik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through it quickly doesn't show any major wrongs with backporting so should be good

@ephys
ephys merged commit 0d0aade into v6 Apr 12, 2022
@ephys
ephys deleted the ephys/v6-backports branch April 12, 2022 14:34
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 6.19.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nholmes3

Copy link
Copy Markdown
Contributor

hey @ephys and @WikiRik

I am upgrading from version 6.17.0 to version 6.21.1 (to include my fix for Postgres schemas) and the changes included in version 6.19.0 break my code in a couple places in a way that I can't quite wrap my head around...

My typescript version is 4.6.2

Break 1:
id is of type string

const instance = await this.scope(null).findOne({ where: { id } });

// this leads to the following typescript output:

error TS2769: No overload matches this call.
  Overload 1 of 2, '(this: ModelStatic<T>, options: NonNullFindOptions<Attributes<T>>): Promise<T>', gave the following error.
    Type '{ id: string; }' is not assignable to type 'WhereOptions<Attributes<T>>'.
      Types of property 'id' are incompatible.
        Type 'string' is not assignable to type 'WhereAttributeHashValue<Attributes<T>[string]>'.
  Overload 2 of 2, '(this: ModelStatic<T>, options?: FindOptions<Attributes<T>>): Promise<T>', gave the following error.
    Type '{ id: string; }' is not assignable to type 'WhereOptions<Attributes<T>>'.
      Types of property 'id' are incompatible.
        Type 'string' is not assignable to type 'WhereAttributeHashValue<Attributes<T>[string]>'.

Break 2:

await this.update(values, {
    where: { id: { [Op.in]: queue.splice(0, max) } },
});


// this leads to the following output:

error TS2769: No overload matches this call.
  Overload 1 of 2, '(this: ModelStatic<...>, values: { [key in keyof Attributes<T>]?: Fn | Col | Literal | Attributes<T>[key]; }, options: Omit<UpdateOptions<Attributes<T>>, "returning"> & { ...; }): Promise<...>', gave the following error.
    Type '{ id: { [OpTypes.in]: string[]; }; }' is not assignable to type 'WhereOptions<Attributes<T>>'.
      Types of property 'id' are incompatible.
        Type '{ [OpTypes.in]: string[]; }' is not assignable to type 'WhereAttributeHashValue<Attributes<T>[string]>'.
          Types of property '[Op.in]' are incompatible.
            Type 'string[]' is not assignable to type 'undefined'.
  Overload 2 of 2, '(this: ModelStatic<...>, values: { [key in keyof Attributes<T>]?: Fn | Col | Literal | Attributes<T>[key]; }, options: UpdateOptions<Attributes<T>>): Promise<...>', gave the following error.
    Type '{ id: { [OpTypes.in]: string[]; }; }' is not assignable to type 'WhereOptions<Attributes<T>>'.
      Types of property 'id' are incompatible.
        Type '{ [OpTypes.in]: string[]; }' is not assignable to type 'WhereAttributeHashValue<Attributes<T>[string]>'.
          Types of property '[Op.in]' are incompatible.
            Type 'string[]' is not assignable to type 'undefined'.

I would greatly appreciate any guidance you can offer on how to successfully upgrade past version 6.19.0

Thanks!

@WikiRik

WikiRik commented Jul 22, 2022

Copy link
Copy Markdown
Member

@nholmes3
I'm not the most comfortable with the typings, but these are my thoughts on it.

Break 1:
In the WhereAttribueHash type (type is the same for v6) I do see in the examples that you need a key-value pair. So instead of looking for one that has an id, you should look for one field where the id equals a specific value. I'm not so sure how well this worked in the past, but something like { id: { [Op.not]: null } } should work as well.

Break 2:
Looking at the Type 'string[]' is not assignable to type 'undefined'. I have the idea that queue and/or queue.splice(0, max) is able to return undefined which we do not allow. So you might have to move that out of the statement and make a variable of it so it's only in the update call if it's not undefined

@nholmes3

Copy link
Copy Markdown
Contributor

@WikiRik thanks for your response!

For break 1 I have tried:

const instance = await this.scope(null).findOne({ id: { [Op.eq]: id } });

which does not work

For break 2 I have tried to separate out the variable for queue.splice with as string[] which still does not work

@ephys are you able to provide any feedback here?

I appreciate your time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants