-
Notifications
You must be signed in to change notification settings - Fork 37
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
Can't get scope abstract constructors to typecheck #120
Comments
Nevermind for now, different version. |
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? |
This should be resolved with my fix that was merged some time ago. |
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 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>'. |
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: |
@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? |
I think it doesn't like the You might want to manually assert that the value is not undefined via |
Wait, is |
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:
|
Thanks for helping out with this! Using that override would mean adding that code to all of our models though right? |
@danprince all models you call |
Ah. I see, but then isn't this broken for everyone? |
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 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 ? |
Ideally |
The generics workaround I posted works pretty well. |
Trying to get scopes working with these typing and running into a confusing type error. Here's a minimal example that seems to fail with the following versions:
[email protected]
[email protected]
types/sequelize#2d9120f
Similarly to #112, I would expect
typeof Model
to benew () => Model
, so I'm not sure why this intersection type fails.The text was updated successfully, but these errors were encountered: