-
Notifications
You must be signed in to change notification settings - Fork 39
Conversation
/cc @vmx |
This change adds flow comment syntax & runs flow check on lint that will fail on type missmatches. Additionally more type-friendly `CID.matchCID` alternative to `CID.isCID` is added which returns CID if value is CID or void otherwise. Method `.equals` is updated so parametre can by an arbirtary value, not just CID. It makes working with CIDs & type-checker a lot easier as it avoids refining `v` to `CID` type before being able to call `cid.equals(v)`.
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.
Can you please explain in which cases matchCID()
is better? I'm a bit hesitant to add new methods to the public API that are similar to the existing API.
package.json
Outdated
@@ -5,7 +5,7 @@ | |||
"leadMaintainer": "Volker Mische <[email protected]>", | |||
"main": "src/index.js", | |||
"scripts": { | |||
"lint": "aegir lint", | |||
"lint": "aegir lint && flow check --color=always", |
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.
I'd like to start slow. So type checking would be its own command type-check
or so. This way it also won't run on CI yet. I'd like to address @mikeal concerns about new contributors. I'd like to add the type check as separate check so that it's also clear to contributors that their code basically works and that it's just the types.
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.
Can we add this to at least "release" script ?
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.
We could even add it to CI, I just want it to be a distinct test step.
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.
@vmx so I've added "check": "flow check --color=always"
. I would like it to run before pull is being submitted, however I'm not exactly sure what the convention here is (I tend to have npm test
setup to do all the checks) so please advice.
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.
Never mind, I've figured it out 1bc1670
.prettierignore
Outdated
@@ -0,0 +1,2 @@ | |||
**/*.js |
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.
Can you please remove this file, I don't want to clutter the root directory too much.
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.
Yeah sorry that was a mistake, just had to stop vscode from reformatting everything & forgot to omit it from commit
So when type-checker sees code like below where if (CID.isCID(x)) {
x //: Buffer|CID
} If instead it was something like: if (x instanceof CID) {
x//:CID
} That is because calls to some functions can’t refine type of the bindings in the caller. However functions can refine type and return refined type, which is what this CID.matchCID does var cid = CID.matchCID(x)
// now flow knows it’s type is `?CID`
if (cid) {
cid //:CID because it’s not void
} |
I would like to address @hugomrdias comment here #83 (comment) because I don't want to further derail API change proposal into typing debate
I agree that flow team has being really bad & at this point in time I would probably prefer TS myself even though type system is significantly less expressive. However adopting TS is far more costly because you have to buy-into whole tooling / syntax. With flow type comments bar is dramatically lower as you just add comments. As of JSDOC it's not even remotely the same, it better than nothing but I'd definitely won't call it a type checker. What we do in this pull request is actually both. |
Not anymore. @hugomrdias talks about TypeScript via JSDoc Syntax. It's like flows inline comments (just not inline). |
One can do the same type checking as normal ts, generated documentation and more. |
@Gozala: FYI, the |
I'm aware, but that JSDoc syntax is even more limited e.g. you can't say define / expose interface. Nor I see how you would define generic methods. I might be biased towards flow as I find JSDoc syntaxt to be difficult to comprehend. I will understand if choice is made in favor of TS. |
I understand that even if it was plain static method as: class CID {
isCID(x) {
return x instanceof CID
}
} It still would refine |
I also don't like the JSDoc syntax :) |
You can define interfaces and import them where ever you want and you can use ts syntax instead of jsdoc in the comments. |
I'm not trying to campaign for TS, just sharing what I learned about it. |
@vmx I have addressed all the comments. Please let me know what would you like to do with this. |
@Gozala I still want to give flow a try. Though I'd like to have the |
Alright, I’ll make a change to remove it then. I just wanted to avoid introducing a friction that could lead to a bad experience with a type checker. I do understand your argument. |
IPFS is now using TypeScript in JSDocs, for more information see ipfs/js-ipfs#2945. Hence there's no point of introducing Flow here anymore. |
This change adds flow comment syntax & runs flow check on lint that will fail on type missmatches.
Additionally more type-friendly
CID.matchCID
alternative toCID.isCID
is added whichreturns CID if value is CID or void otherwise.
Method
.equals
is updated so parametre can by an arbirtary value, not just CID. It makesworking with CIDs & type-checker a lot easier as it avoids refining
v
toCID
type beforebeing able to call
cid.equals(v)
.--
I'm using
flow check
right now but that should probably be moved to aegir ipfs/aegir#347. I suggest to address that separately.