Breaking: Revamp TypeScript declarations#810
Breaking: Revamp TypeScript declarations#810tinovyatkin merged 18 commits intonode-fetch:masterfrom tinovyatkin:test-types
Conversation
There was a problem hiding this comment.
LGTM w/ a few comments
Have you considered using tsd to test the type definitions, instead of just using tsc. It gives you expectType<T>() so that you can actually check that retured types are what you expect.
e.g.
// ok must be boolean, so do strict equality
if (getRes.ok !== true) ...will not raise a warning if ok was typed as true | number | 'foobar', since it's still valid to compare it with true. But with tsd you would be able to do:
expectType<boolean>(getRes.ok)
if (getRes.ok !== true) ...It also supports top-level await so you can remove async function run :)
Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>
|
@LinusU I didn't want to introduce a new testing framework for types and resort to this kind of "functional testing" by compilation a file via the latest version of TypeScript on the GitHub Actions. |
|
Referencing dom lib causes all sorts of problems for node projects. This has been an ongoing problem with superagent for a long time. Please reconsider this approach. See: DefinitelyTyped/DefinitelyTyped#12044 (comment) There are some very painful conflicts like the timer interfaces and globals such as URL. I recently switched from superagent to node-fetch to avoid these problems. |
|
@jstewmon I should agree that lack of module isolation (by default) in TypeScript may cause background injection of I will bring inside all our types (at better review it's not that much we need to bring in). |
|
@LinusU we probably need to revert node-fetch/fetch-blob@bec5a3d |
Hmm, yeah, we can probably copy the classes from
The types should always reflect what's in the source file, and since this is the source: module.exports = BlobThe correct typing is |
index.d.ts
Outdated
| highWaterMark?: number; // =16384 the maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. | ||
| import { Agent } from 'http'; | ||
| import { URL, URLSearchParams } from 'url' | ||
| import { AbortSignal } from 'abort-controller' |
There was a problem hiding this comment.
abort-controller is not a dependency so it won't work out of the box. Also, node-fetch is supposed to be a lightweight implementation and adding this might be weighing it down.
There was a problem hiding this comment.
@Richienb actually abort-controller is a devDependency of this project for quite a long time. What will be your proposed alternative for referring AbortSignal here - bring it from lib.dom or copy the whole definition inside (it's quite big)?
There was a problem hiding this comment.
Is it possible to import from lib.dom using ES6 imports?
There was a problem hiding this comment.
Nope, lib.dom is a library, not a module. And you probably should double-check the @jstewmon comment above about downside of just referring lib.dom in a triple-slash comment.
Richienb
left a comment
There was a problem hiding this comment.
The files in @types can probably live in the root directory.
|
I'm OK to move them whatever place the team will decide, but what's particularly wrong with |
|
For me, it doesn't matter where the type tests will be placed ;) |
|
Are we ready to merge or not yet? |
xxczaki
left a comment
There was a problem hiding this comment.
Actually, I think we need to document this change in the upgrade guide.
|
For me this is pretty much done and ready for merge. The fact that v3.0 comes bundled with types is documented on README at TypeScript section. |
Richienb
left a comment
There was a problem hiding this comment.
The @ symbol in @types is unnecessary. I would personally call it typings but types would do.
|
Let's change the name to |
|
That's a great Sunday discussion on one symbol in the folder name at project internal structure 😄 @xxczaki I don't have statistics (would be nice to have) how many projects have it at |
|
@tinovyatkin I mean, if one symbol improves DX just a bit and does not really affect any users, we can go for it 😆 |
|
Is also support @types |
|
I have one question In the actions file don’t we need to specify the node version |
|
We should change the name CI in the actions to something more specific b/c we already have a CI yml |
No, GitHub Actions have LTS version installed by default: https://help.github.com/en/actions/reference/software-installed-on-github-hosted-runners
No, we shouldn't unless we want more badges in README. GitHub Action will sum up action by this name into one badge status, so, if somebody will push a broken typing into |
|
Alright 🦄 |
node-fetch/node-fetch#1141 and node-fetch/node-fetch#810 should be proof these lads are high on acids
node-fetch/node-fetch#1141 and node-fetch/node-fetch#810 should be proof these lads are high on acids
node-fetch@v3 has bundled types: node-fetch/node-fetch#810
* Update @heroicons/react * Update @types/node * Update @types/react-paginate * Update node-fetch (v2 → v3) * Remove @types/node-fetch node-fetch@v3 has bundled types: node-fetch/node-fetch#810 * Update typescript * Update npm lockfile to v3 * Update tailwindcss, postcss, autoprefixer * Update tailwind config for v3 * Change classnames to tailwind v3 equivalents * Fix post-update heroicons issues * Update react-hook-form * Update react-paginate * Update prettier * Update eslint-* minor versions * Update @types/react minor version * Update next-themes * Update node-fetch * Update @typescript-eslint/* * Update eslint* * Update .gitignore * Remove next-env.d.ts Remnant of older installation. https://nextjs.org/docs/basic-features/typescript#existing-projects * Update next (v11 → v12) * Update @types/react* * Update next (v12 → v13) * Add eslint-config-next * Update eslint config Remove redundant eslint config. Sane defaults are being applied by (eslint-config-next)[https://nextjs.org/docs/basic-features/eslint#eslint-config]. * Update next config, fix issue with images src * Remove redundant eslint plugins See 8dbd0d5 * Use next components instead of plain html * Remove @types/react-paginate Types are now bundled. * Remove node-fetch * Slightly refactor api response usage



The decision to bring types declaration bundled since version 3.0 is a good move, however, it also makes maintainers of the project responsible for the quality of declarations.
Unfortunately, current typing has several problems:
AbortSignalwithout including reference to thedomlib, so, it will not compile in a project that doesn't have it included (see the included test as an example).referreron the body whileREADMEexplicitly says we don't have thatgetAllon Headers while we don't have such thing in code anymoreRequestContextorResponseType, and exports things that we actually don't export.So, this PR proposes completely different, lightweight, and minimal approach - it uses types, declared in DOM library included with TypeScript, and extends or shrinks them whether possible and required. The resulted declaration is smaller, more compatible, and easier to maintain and read.
As it dedicated to version 3.0 it's OK to have some breaking changes and it's includes breaking functionality for users migrating from
@types/node-fetch, most notable:response.json()method returnsunknown, notany. It's a good practice and even a reference example of a use forunknowntype when it was introduced:This is useful for APIs that want to signal “this can be any value, so you must perform some type of checking before you use it”. This forces users to safely introspect returned values.
Request,Response,Headers,FetchError,AbortErrorandfetch(default export) itself. Nothing else (the previous version was product in itself, exporting evenRequestContextenum that we don't have in code).To guarantee our typing declaration consistency I also included small automated test for GitHub action.
PS: It doesn't include any changes to JS code, so, Travis is just flacky.