-
-
Notifications
You must be signed in to change notification settings - Fork 334
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
Move to TypeScript #330
Move to TypeScript #330
Conversation
c85e8b5
to
2610fff
Compare
Hi @mgcrea and thanks for your effort. I'll do a proper review once Sindre chimes in with whether he wants to move forward with this (given the comments on #321) and the tests are passing. I don't have strong feelings myself, except to say that - as you've encountered - some of the patterns we use in Ky don't seem to be fully supported by TypeScript and I dislike writing code differently than we would otherwise just to please the compiler. Especially when it comes to the public API, which seems to be the case here with the static That said, for Deno users like myself this would be awesome. The current approach to loading type definition files in Deno is kludgy and non-standard, so I'd love to remove the need for that. |
@sholladay thanks! If I'm not mistaken, Waiting for @sindresorhus blessings to continue with unit tests. |
Correct, the Ky class is an internal concept. I thought that making |
I'm ok with moving forward with this as the code is complicated and it's nice having some type safety when refactoring or adding features. |
What's the plan for testing?
|
@sindresorhus Haven't really thought about it yet but I guess we could use a specific tsconfig build for tests (without ESM) along with I'm usually using Will try to have something working pretty soon. |
I guess that might work.
ESM is the only production target. |
e4116ac
to
d3b96e8
Compare
@sindresorhus I've just finished to fully migrate the tests to typescript. Was a bit painful to find the exact working setup to properly test the ESM build from non-ESM compiled unit-tests that nyc/ava can digest, but ended up finding something thanks to playwright debug mode. I did bump all dev deps, so I had to fix a few linting-related issues in the code to have Had to rewrite a bit the helpers part related to I went with named exports across the source code Also had to add If you find any issue blocking the merge don't hesitate to ping me and I'll do my best to address it / fix it in a timely manner. Another thing we should decide before publishing a release is the tsc compiler target, currently |
source/utils/merge.ts
Outdated
const isHeadersInstance = source2 instanceof globalThis.Headers; | ||
const source = new globalThis.Headers(source2 as HeadersInit); | ||
|
||
// eslint-disable-next-line unicorn/no-array-for-each |
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.
Adhere to the linting.
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.
Unfortunately typescript won't let me use a for-of loop in that case, getting Type 'Headers' must have a '[Symbol.iterator]()' method that returns an iterator.
and no entries()
method declared.
edit
: fixed by adding missing "DOM.Iterable" to tsconfig.json lib.
package.json
Outdated
"unicorn/import-index": "off", | ||
"import/extensions": "off" | ||
"@typescript-eslint/dot-notation": "off", | ||
"@typescript-eslint/quotes": "off", |
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.
Don't disable this rule.
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.
Done for quotes
, have to keep dot-notation
off as typescript forbids dot notation in quite a few header-related cases (due to specific type) and disabling by line would be tedious/verbose.
Commented on some nitpick. Will do a more in-depth review tomorrow. |
|
31982f2
to
47ac855
Compare
Looking at the code today I realised that during my tests I had dropped the Hopefully nyc/ava adds support for ESM soon so we can simplify the build/test. |
Regarding testing, seems there's a solution with |
Just finished implementing the solution you linked and it drastically simplified the testing process as it no longer requires building the tests (so no more dist/ folder) and no package.json monkey-patching needed! One downside however is that we can no longer test against NodeJS v12, but since it's a browser lib I'm not sure there is much benefit testing against a matrix of NodeJS versions (ideally we should test against a matrix of browsers). |
562e941
to
5020183
Compare
We do support Node when combined with polyfills, but I'm fine with dropping Node 12 from the test matrix.
That's what |
👍 |
@sindresorhus I've dropped the |
@mgcrea Don't force push. It makes it impossible to review only what changed. |
@mgcrea Did you see @sholladay's feedback? |
Yes and I think I've answered every comment, to me there is nothing really blocking left (but I could be wrong). Point taken regarding force-pushing, won't do it again. |
I don't see those responses to my comments, @mgcrea. |
source/utils/merge.ts
Outdated
const isHeadersInstance = source2 instanceof globalThis.Headers; | ||
const source = new globalThis.Headers(source2 as HeadersInit); | ||
|
||
// eslint-disable-next-line unicorn/no-array-for-each |
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.
Unfortunately typescript won't let me use a for-of loop in that case, getting Type 'Headers' must have a '[Symbol.iterator]()' method that returns an iterator.
and no entries()
method declared.
edit
: fixed by adding missing "DOM.Iterable" to tsconfig.json lib.
Indeed it looks like I did not properly understand how the reviewing process works, all my comments were flagged as pending and I thought you could see them. I had to « submit review » to properly publish them...! 😅 Good to know. I also marked every conversation as resolved as an earlier attempt to publish so feel free to unresolve the points you want to have addressed / discussed. |
For the remaining We can link to https://github.com/sindresorhus/ky/search?q=%22@ts-expect-error%22 |
@mgcrea Would you be able to fix the failing tests? |
@sindresorhus forgot to push a commit fixing the broken test due to the updated dist path, should be ok now. Looks like the workflow is stuck (new security feature of GitHub) pending your approval. |
@mgcrea Thanks for working on this 👍 |
Thanks for merging such a big PR! Looking forward to contribute with smaller patches in the future! |
Following #321, I took some time today to attempt a port to TypeScript with minimal changes.
Work is not fully done yet (did not update the CI pipeline, hence the failed tests), but it does pass typescript validation.
There is a two places where I had to
@ts-expect-error
, related to dynamically assigned props as I'm not sure yet how I can port it to Typescript (could be done easily by assigning each props without the loop, see reviews). Open to ideas on this.Also there is some remaining typing issues (existing in the current code but invisible) regarding the actual options passed around (see
InternalOptions
vsNormalizedOptions
) so I had to add a few manualas
casting as well that should be fixed (but could be done after the migration to keep changes to a minimum).Also I had to refactor the Ky class API a bit (added a static
Ky.create
) that did return aResponsePromise
from its constructor instead of the expectedKy
instance as it looks like TypeScript forbids class constructor return type overloads.I tried my best to follow what was made for
got
and to not change anything in the code yet (I have quite a few changes that I'd like to propose next but that were out of scope).Before I spend too much more time on this I'd love to get some feedback on the direction of this merge request and the probability of it being merged once ready.
I'm willing to change anything that you would not like (naming, etc.).
Thanks for the review!