Git Product home page Git Product logo

Comments (17)

SimonSchick avatar SimonSchick commented on September 22, 2024

#110 probably related.

Nevermind for now, different version.

from sequelize.

danprince avatar danprince commented on September 22, 2024

Just wanted to check in on this one again. This error is meaning that we still can't use scopes in our codebase. Are there any examples of scope code typechecking correctly or is this just something that wontfix for this combination of versions?

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

This should be resolved with my fix that was merged some time ago.

from sequelize.

danprince avatar danprince commented on September 22, 2024

Afraid not @SimonSchick. Just trying out a few commits between your merge and HEAD.

TypeScript 2.3.4, 2.4.0 and 2.4.2:

let users = await User.scope("human").findAll({ where: { team_id: teamId } });

// The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type '(new () => Model) & typeof Model'.
//   Type 'typeof Model' is not assignable to type 'new () => Model'.
//     Cannot assign an abstract constructor type to a non-abstract constructor type.

TypeScript 2.4.0:

error TS2345: Argument of type '{ where: { "slack.name": string | undefined; }; }' is not assignable to parameter of type 'FindOptions | undefined'.
  Type '{ where: { "slack.name": string | undefined; }; }' is not assignable to type 'FindOptions'.
    Types of property 'where' are incompatible.
      Type '{ "slack.name": string | undefined; }' is not assignable to type '(string | number)[] | Where | WhereAttributeHash | AndOperator | OrOperator | undefined'.
        Type '{ "slack.name": string | undefined; }' is not assignable to type 'OrOperator'.
          Property '$or' is missing in type '{ "slack.name": string | undefined; }'.

Why would the FindOptions require an $or clause in the where object? I would have expected it to match WhereAttributeHash from that union.

TypeScript 2.4.2:

team.set("slack.domain", message.event.name);

// error TS2559: Type '"slack.domain"' has no properties in common with type 'Partial<Team>'.

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

TS attempts to match your type to all listed types and will end up telling you only about one mismatch, the interesting bit is here: (string | number)[] | Where | WhereAttributeHash | AndOperator | OrOperator | undefined.

from sequelize.

danprince avatar danprince commented on September 22, 2024

@SimonSchick I'd understand that if it was an intersection type, but why would it report an error when one of the types in the union does match?

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

I think it doesn't like the undefined type union, you probably have strict null checks enabled...

You might want to manually assert that the value is not undefined via if or suffix the index with a ! eg. where: { team_id: teamId! } } as only null is allowed in the WhereAttributeHash type.

from sequelize.

danprince avatar danprince commented on September 22, 2024

I do have strict null checks enabled, but the error is thrown regardless of which method I try to invoke on User.scope().

image

I've just turned them off and get the exact same compile errors.

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

Wait, is User abstract in your code?

from sequelize.

danprince avatar danprince commented on September 22, 2024

Here's a repro: https://gist.github.com/danprince/6dd8566e22c6a82a29cb57f195c2ff0e

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

Ooooh, I see what you mean now, I ran into the same thing.

So here is what you can do:

Add a override for all scopes you have to your model.

Eg.

public static scope(options?: ScopeStringType | ScopeStringType[] | ScopeOptions | WhereAttributeHash): typeof YourModel {
    return <typeof YourModel> super.scope(options);
  }

Also your scope is wrong, should probably be:

scopes: {
  human: {
    where: {
      slack_id: { $ne: 'USLACKBOT' }
    },
  },
},

from sequelize.

danprince avatar danprince commented on September 22, 2024

Thanks for helping out with this! Using that override would mean adding that code to all of our models though right?

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

@danprince all models you call .scope static on, yes.
The problem here is that the base .scope has the return signature typeof Model whereas Model is abstract.

from sequelize.

danprince avatar danprince commented on September 22, 2024

Ah. I see, but then isn't this broken for everyone?

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

Yes and no, I assume @felixfbecker intention was for this to be overridden on a per model basis.

An alternative would be to change to call signature of .scope to something more generic such as:

export interface ModelConstructor {
  new (...args: []): Model;
}
...
abstract class Model {
  ...
  static scope<M extends ModelConstructor>(this: M, options?: string | Array<string> | ScopeOptions | WhereAttributeHash): M;
}

Thoughts @felixfbecker ?

from sequelize.

felixfbecker avatar felixfbecker commented on September 22, 2024

Ideally scope should return typeof this or typeof this['constructor'] but I don't think that's possible. It might be possible to use generics as a workaround, although not pretty.

from sequelize.

SimonSchick avatar SimonSchick commented on September 22, 2024

The generics workaround I posted works pretty well.

from sequelize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.