Comments (17)
#110 probably related.
Nevermind for now, different version.
from sequelize.
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.
This should be resolved with my fix that was merged some time ago.
from sequelize.
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.
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.
@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.
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.
I do have strict null checks enabled, but the error is thrown regardless of which method I try to invoke on User.scope()
.
I've just turned them off and get the exact same compile errors.
from sequelize.
Wait, is User
abstract in your code?
from sequelize.
Here's a repro: https://gist.github.com/danprince/6dd8566e22c6a82a29cb57f195c2ff0e
from sequelize.
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.
Thanks for helping out with this! Using that override would mean adding that code to all of our models though right?
from sequelize.
@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.
Ah. I see, but then isn't this broken for everyone?
from sequelize.
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.
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.
The generics workaround I posted works pretty well.
from sequelize.
Related Issues (20)
- it's necessary to execute a find operation to get models works? HOT 2
- [ts] Module '"/node_modules/@types/sequelize/index"' has no exported member 'Modal' HOT 1
- why is this separate HOT 3
- Missing symbol operators export
- Model.count: CountOptions has no attribute 'paranoid' HOT 2
- interface incomplete: InitOptions
- [TypeScript] Typing the Promise returned by a query HOT 1
- BelongsToCreateAssociationMixin Return Type Void in NPM
- How can I set options.initialAutoIncrement, the interface of options doesn't have prop initialAutoIncrement
- AssociationOptionsHasOne does not have scope HOT 1
- [v5] sourceKey missing from HasOneOptions HOT 1
- [v5] sequelize.import error TS4082: Default export of the module has or is using private name 'Bluebird' HOT 1
- [v5] Missing typings for 4 common sequelize data types
- findByPk is missing in types/sequelize HOT 1
- QueryInterface missing Schema Name HOT 1
- Cannot extend Model like in README.md
- Error TS1086: An accessor cannot be declared in ambient context. HOT 2
- Update should accept a Partial Model as first parameter
- SyncOptions interface doesn't container alter
- find** methods with { raw: true } should has another types?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sequelize.