-
Notifications
You must be signed in to change notification settings - Fork 26
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
Refactor to make code more TypeScript compatible #183
Conversation
This change contains some refactors, mostly to make the code better compatible with TypeScript. All `require()` calls were changed to the form `const identifier = require('specifier')`. This is compatible with TypeScript’s `verbatimModuleSyntax` option, which is strongly recommended in order to produce valid library types. All uses of `lodash` were changed to native JavaScript implementations. The `lodash` dependency was removed. Static class assignments were changed to static class property syntax. `util.promisify` usage was replaced with Node.js promise APIs.
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 have to be careful now that we no longer run CI tests for the supported node versions 16 and 14, that features like fs/promises, stream/promises and Object.fromEntries are avail those node versions. Or maybe we'll just say that we make this TypeScript rewrite a major version so we don't have to worry about this anymore.
let uploadedBytes = 0 | ||
for (const l of streamLabels) { | ||
uploadedBytes += uploadProgresses[l] | ||
} |
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 will probably err in typescript when strict null checks is enabled. maybe better to do a streamLables.reduce(...)
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.
Why would this result in a TypeScript error? Reduce would definitely not help with this.
I strongly believe array.reduce
should never be used.
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.
the following code results in Object is possibly 'undefined'.
typescript error when strict null checks is enabled (which i think makes typescript more accurate):
const progresses: Record<string, number> = { a: 1, b: 2, c:3 }
const labels = ['a', 'b', 'c']
let sum = 0;
for (const l of labels) {
sum += progresses[l]
}
but then this also gives the same error, so ignore what i just suggested 🤦♂️
const sum = labels.reduce((acc, l) => acc + progresses[l], 0)
I misread your code and instead read it as a simple sum of elements. in that case this code does not produce the same ts error:
const sum = numbers.reduce((acc, n) => acc + n, 0)
but again ignore what i said.
as for using array.reduce
i agree that it should be used sparingly, but as mentioned in your link it's nice for summing numbers:
It's only somewhat useful in the rare case of summing up numbers, which is allowed by default.
I agree. I double checked all their availabilities in the docs.
I’m ok with this, but I don’t think it’s needed. I would like to replace the |
ok, all good then! |
This change contains some refactors, mostly to make the code better compatible with TypeScript.
All
require()
calls were changed to the formconst identifier = require('specifier')
. This is compatible with TypeScript’sverbatimModuleSyntax
option, which is strongly recommended in order to produce valid library types.All uses of
lodash
were changed to native JavaScript implementations. Thelodash
dependency was removed.Static class assignments were changed to static class property syntax.
util.promisify
usage was replaced with Node.js promise APIs.