-
Notifications
You must be signed in to change notification settings - Fork 280
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
Type inference for static Model methods #189
Conversation
adds type inference to most static Model methods (create(), findAll(), etc) that previously required user-supplied generic parameters to correctly produce the correct return type.
Codecov Report
@@ Coverage Diff @@
## master #189 +/- ##
=======================================
Coverage 96.85% 96.85%
=======================================
Files 100 100
Lines 921 921
Branches 125 125
=======================================
Hits 892 892
Misses 9 9
Partials 20 20
Continue to review full report at Codecov.
|
lib/models/Model.d.ts
Outdated
|
||
/** | ||
* Builds a new model instance and calls save on it. | ||
*/ | ||
static create<T extends Model<T>>(values?: any, options?: ICreateOptions): Promise<T>; | ||
static create<T extends Model<T>, A>(values?: A, options?: ICreateOptions): Promise<T>; | ||
static create<T extends Model<T>>(this: (new () => T), values?: T, options?: ICreateOptions): Promise<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be values?: any
instead of values?: T
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. I'm not sure how that got flipped! Will revise.
Hey @schmod really really good feature. Didn't know that this is possible. Thank you for implementing this. I only found one issue in the typings: |
|
Adds type inference to most static methods on
Model
and its subclasses (create()
,findAll()
, etc) that previously required user-supplied generic parameters to correctly yield the correct return type.Person.findAll<Person>
simply becomesPerson.findAll()
, and TypeScript will automatically infer the correct return type. The old type signature will also continue to work. See microsoft/TypeScript#5863 for an explanation of what's going on under the hood that allows this to work.New overloads have been added to
Model.create<T extends Model<T>, A>(vals: A) : T
and its cousins, so thatModel.create<A>(vals: A) : T
may also be used in as a shorthand for when developers want to provide strict typings forA
, but don't need to override the automatic inference ofT
.Updates tests to run against the current version of Sequelize v4, and uses newer Bluebird type definitions (a milder version of #166 until we can figure out what to do there -- necessary because the older version had an inaccurate definition of
Promise.all
).