You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why don't we have generic for the model attributes?
e.g.
interfaceTypedModelAttributes{id: number;optionalData?: object;}classTypedModelextendsModel<TypedModelAttributes>{
...
}// usageTypedModel.build({ ... })// will be type-checked by "TypedModelAttributes"TypedModel.findOne({where: { ... })// will be type-checked by "keyof TypedModelAttributes"
...
It is more accurate and will bring a lot of conveniences than current <K extends keyof this> way.
aMethod and anotherMethod can be treated as model's attributes in the <K extends keyof this> scenario.
Furthermore, the model prototype's own method (such as get, set, toJSON, ... ) also treated as their attributes, so it makes the intellisense (quick suggestions) bulky with unnecessary values.
I don't think it's that big of an issue that you can also pass the methods. I don't think that would cause any error in real-world usage. On the other hand, having to duplicate the attribute declaration in both the class and an interface is cumbersome.
Though writing same attribute declaration in class and interface is bit cumbersome, it's not a requirement. We can also write a model without generic thanks to the TypeScripts' default type parameters.
Like @SimonSchick said, generic attributes will provide more help when we write where or attributes clause, also allowing any sequelize.fn type too.
If you don't mind, I'll try to make a suggestion PR from below scratch.
typeWhereAttributeHash<TAttributes=any>={[KinkeyofTAttributes]?: TAttributes[K]|WhereValue<TAttributes[K]>;};typeWhereValue<TAttributes=any>=TAttributes|null|WhereOperators<TAttributes>|WhereAttributeHash<TAttributes>// for JSON columns|Col<TAttributes>// reference another column|OrOperator<TAttributes>|AndOperator<TAttributes>|WhereGeometryOptions<TAttributes>|Array<TAttributes|WhereAttributeHash<TAttributes>>;// implicit $ortypeWhereOptions<T=any>=WhereAttributeHash<T>|null|T|T[];
Activity
felixfbecker commentedon Sep 5, 2017
Why do you think is it more accurate than this?
The attributes are attributes of the class, so imo they should be declared there.
What conveniences will it bring exactly?
rokoroku commentedon Sep 5, 2017
when we define custom model method
aMethodandanotherMethodcan be treated as model's attributes in the<K extends keyof this>scenario.Furthermore, the model prototype's own method (such as
get,set,toJSON, ... ) also treated as their attributes, so it makes the intellisense (quick suggestions) bulky with unnecessary values.SimonSchick commentedon Sep 11, 2017
Making the attributes a model would also properly restrict
attributes: [...], and allow.get()to return correct objects.felixfbecker commentedon Sep 11, 2017
I don't think it's that big of an issue that you can also pass the methods. I don't think that would cause any error in real-world usage. On the other hand, having to duplicate the attribute declaration in both the class and an interface is cumbersome.
rokoroku commentedon Sep 11, 2017
Though writing same attribute declaration in class and interface is bit cumbersome, it's not a requirement. We can also write a model without generic thanks to the TypeScripts' default type parameters.
Like @SimonSchick said, generic attributes will provide more help when we write
whereorattributesclause, also allowing anysequelize.fntype too.If you don't mind, I'll try to make a suggestion PR from below scratch.
SimonSchick commentedon Sep 24, 2017
One way to achieve this without massive duplication would be: