fix(types): add missing transaction typescript types (v6-beta)#11620
fix(types): add missing transaction typescript types (v6-beta)#11620sushantdhiman merged 2 commits intosequelize:masterfrom
Conversation
|
Question, if this ends up getting merged, would it be added to all versions? (v4, v5, and v6-beta) or only to the latest stable one (v5)? |
Codecov Report
@@ Coverage Diff @@
## master #11620 +/- ##
=======================================
Coverage 96.26% 96.26%
=======================================
Files 94 94
Lines 9190 9190
=======================================
Hits 8847 8847
Misses 343 343Continue to review full report at Codecov.
|
|
Hello! I see you are a first-time contributor, thank you for taking the time to help Sequelize! I hope to see more PRs from you in the future! I will now review your PR :) |
| UPDATE: LOCK.UPDATE; | ||
| SHARE: LOCK.SHARE; | ||
| KEY_SHARE: LOCK.KEY_SHARE; | ||
| NO_KEY_UPDATE: LOCK.NO_KEY_UPDATE; |
There was a problem hiding this comment.
Hello, I have a question, I don't use TypeScript much, why is both an enum and an interface necessary? Also why is only the enum being exported?
There was a problem hiding this comment.
Also why did you move the enum outside the export namespace Transaction block?
There was a problem hiding this comment.
So, if sequelize was entirely written in TS (and not only types were provided), we could just use an enum directly in the code and that would get compiled to an actual object (This is actually discouraged by some people and they advise to do this if you want to support runtime enums: https://medium.com/@martin_hotell/10-typescript-pro-tips-patterns-with-or-without-react-5799488d6680, look at point 3).
But, since here we are just providing types and not touching the code, we can't just do that. Here, an enum is just a TS type that basically says "look, whatever you say has this type, can only be one of these values". In our case, LOCK is an enum with 4 possible values which we're saying are strings. Now, we might first think that the static get LOCK and the get LOCK would return a type of LOCK (which is an enum). But that would not be correct by looking at the code. The actual code returns an object with four properties, UPDATE, SHARE, KEY_SHARE and NO_KEY_UPDATE. So, for our types to be correct, we need to tell static get LOCK and get LOCK that they return an object with those four properties, therefore the need for the interface LOCK, which models exactly that.
We only export the enum because we use it as one of the possible types for lock in the models file. The interface is just used in this file to indicate what the getter functions return.
I moved the enum outside the namespace because then it would conflict with the type declaration of the actual class getters also called LOCK.
papb
left a comment
There was a problem hiding this comment.
Please add at least one test to make sure your implementation works as intended and to prevent regressions in the future.
Testing the TypeScript typings consists simply on checking if some code can be compiled. In other words, a typings test is just some TS code that should compile. What it does when executed is irrelevant, the test is simply to compile it without errors, as can be seen in our CI configuration (using npm run test-typings) here and here. Since it's just a compilation, getting no output from it means that it passed. Getting a compilation error is a failure. Note that if you directly throw an error from your typings test code, the test will still pass because throwing an error is something that compiles just fine.
For good examples on how to write typescript typings, check the following PRs: #11368 #11378 #11520
Let me know if you need further help!
|
@papb added tests to use the |
No. This depends on what branch you opened the PR into. In this case: Since this PR is for I think it's a good idea to have this change on v5 as well, so if you could open another PR with the same changes but targeting the About v4, note that we didn't provide typescript typings by then, so this fix does not apply to v4. Typings for v4 exist but are unofficial and kept in the DefinitelyTyped package called @types/sequelize. |
Ah, nice. I'll open the PR for v5 now. Thanks for all the info/help. |

Pull Request check-list
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
As stated in the sequelize docs (https://sequelize.org/master/class/lib/transaction.js~Transaction.html#static-get-LOCK), when performing find operations, a lock property can be passed, and that can be either
trueorfalseor also a lock level obtained from the actual instance, i.e.t1.LOCK.UPDATE, which ends up being a string.The TypeScript types were failing when trying to pass a boolean value to
lockor when trying to accesst1.LOCK...on a transaction instance.This PR adds those missing types.
Link to existing issue: #11178 (comment)