Skip to content
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): return a usable type when using the sequelize.models lookup #11293

Merged
merged 1 commit into from Aug 9, 2019

Conversation

schmod
Copy link
Contributor

@schmod schmod commented Aug 7, 2019

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Currently, looking up model classes via the sequelize.models interface does not yield an object with a usable type signature (for reasons that I admittedly do not entirely understand).

Static functions such as findAll and belongsTo do not work on the values pulled off of sequelize.models, presumably because these methods use the ModelCtor "hack" to infer the correct return type.

class Model1 extends Model {}

const M1 = sequelize.models.model1;
m1.findAll({});  // <-- ERROR

/**
The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type 'ModelCtor<Model<unknown, unknown>>'.
  Type 'typeof Model' is not assignable to type 'new () => Model<unknown, unknown>'.
    Cannot assign an abstract constructor type to a non-abstract constructor type.ts(2684)
/**

It appears that these methods can be called if sequelize.models is redefined to contain ModelCtors instead of typeof Model.

@codecov
Copy link

codecov bot commented Aug 7, 2019

Codecov Report

Merging #11293 into master will increase coverage by 0.76%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11293      +/-   ##
==========================================
+ Coverage   95.58%   96.34%   +0.76%     
==========================================
  Files          91       94       +3     
  Lines        8855     9057     +202     
==========================================
+ Hits         8464     8726     +262     
+ Misses        391      331      -60
Impacted Files Coverage Δ
lib/dialects/mariadb/index.js 100% <0%> (ø)
lib/dialects/mariadb/connection-manager.js 100% <0%> (ø)
lib/dialects/mariadb/query.js 87.87% <0%> (ø)
lib/dialects/abstract/query-generator.js 97.61% <0%> (+0.17%) ⬆️
lib/sequelize.js 95.95% <0%> (+0.62%) ⬆️
lib/query-interface.js 92.17% <0%> (+1.46%) ⬆️
lib/dialects/mariadb/data-types.js 100% <0%> (+49.01%) ⬆️
lib/dialects/mariadb/query-generator.js 97.87% <0%> (+87.23%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 488c048...c7b2669. Read the comment docs.

@schmod schmod changed the title fix(types): use correct this value in getterMethods and setterMethods fix(types): return a usable type when using the sequelize.models lookup Aug 7, 2019
@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. labels Aug 7, 2019
@papb papb requested a review from SimonSchick August 7, 2019 19:35
Copy link
Contributor

@SimonSchick SimonSchick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok, but we are looking to deprecate .models eventually and I would discourage the use of .models in the future for type safety.

@schmod
Copy link
Contributor Author

schmod commented Aug 7, 2019

Yeah, FWIW, I wouldn't mind if we added some more guidance on how to best use Sequelize in a typesafe manner, particularly with regard to model definitions.

A bunch of the tricks that folks normally use to manage associations without introducing cyclical dependencies don't quite work in the TS world.

@papb
Copy link
Member

papb commented Aug 7, 2019

@schmod What kind of tricks are you referring to? Can you show an example? (No rush, and not relevant to this PR)

@sushantdhiman sushantdhiman merged commit a39c63a into sequelize:master Aug 9, 2019
@sushantdhiman
Copy link
Contributor

🎉 This PR is included in version 5.13.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants