-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
fix(types): allow use of sequelize.define
models in types
#13834
Conversation
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.
Looks good to me, thank you for the extra jsdoc too
* Do not switch the order of `typeof Model` and `{ new(): M }`. For | ||
* instances created by `sequelize.define` to typecheck well, `typeof Model` | ||
* must come first for unknown reasons. |
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.
This is probably because typeof Model
is a constructor and as such, its new
function would take precedence over the constructor of { new(): M }
.
Please don't merge this yet. This issue is related to a problem I've been trying to solve for the past year (ModelCtor not acting just right) and I think I've just solved it by changing // remove the existing constructor that tries to return `Model<{},{}>` which would be incompatible with models that have typing defined & replace with proper constructor.
export type ModelCtor<M extends Model> = Omit<typeof Model, 'new'> & { new(): M }; Checking if tests pass, brb |
@ephys and @AllAwesome the fix in #13890 looks better. Ship it! |
I guess this one can be closed then, right? |
Thanks @hsource :) I'll close this one then |
Pull Request Checklist
Please make sure to review and check all of these items:
npm run test
ornpm run test-DIALECT
pass with this change (including linting)?Description Of Change
Motivation
#13010 had fixed an issue where class-based Models with explicit types couldn't be used in includes and other places that expect static models, but it had inadvertently broken the same for Models created with
sequelize.define()
, which were the standard before it.This is because it created a new
ModelType
type, that required explicit properties.sequelize.define
outputs aModelCtor
type that doesn't conform to that:Code with error:
Error: TS2322: Type 'ModelCtor' is not assignable to type 'ModelType<any, any> | undefined'.
Fix
I wrote the original Typescript typings for v6 (#12405), but didn't document it really well. This change better documents how the types should be used and swaps out most uses of
ModelType
withModelStatic
, which is compatible with both class andsequelize.define
Models.Here's what I did:
ModelCtor
andModelStatic
in the typings fileModelType
to useModelStatic
, which works with both class andsequelize.define
modelsUser.ts
Todos
None