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
node@18.x.x version not fetch global api ? #60924
Comments
I think because it's still experimental |
Yes, node official announce too. |
It was simply not added because of time constraints so hopefully sometime soon... |
@SimonSchick any chance we can get |
Is it available in any branch or development version we can make use of? |
I would love to, but my client asked me to use Typescript :-) |
The problem is still that dom libs are not modularized, if they were node typings could just reference dom specs. |
Agree on that. Can you point us where it's defined so we can copy it ourselves in our projects, meanwhile it get added to Node.js definitions? |
I think it's better to not to use fetch Api in node. But if U need it ( for any reason ) it's seems easy to define one: declare function fetch(...): ...;
If you dont wanna see it in your ts files you can just simply create types.d.ts file ( name is important ) or create a folder Note: your d.ts ( definition file ) must not be a module if it is, you have to import the fetch type in every file that you are using it. |
Why not?
That's what I'm asking for. |
|
I would not say @mcollina code is not good, with so much performance obsesive code done...
Node.js is not from Microsoft, Microsoft has only developed Typescript on top of it. |
Good point, fair enough.
It's the most stable one, just only doesn't have yet the LTS denomination. I could agree that odd versions (v17, v19...) are unstable development versions and should not be used on production, but pair ones (v16, v18) is totally fine. |
Yeah, node 18 will be LTS in two months (2022-10-25).
@SimonSchick, so are we expecting lib.dom to be modularized before we can add node typings for fetch? Sounds like something that might take a while. I wonder if in the meanwhile we could add fetch to types/node by copy-pasting fetch, Request, Response, Headers etc. definitions as a standalone module that would be easy to delete when it's possible to just reference the respective DOM definitions. |
Agree, and add a |
Or just import the |
Any news on this? I'd prefer not to have my global namespace polluted with |
Is there any chance you could install undici as a dep on |
We are not planning to have a stable fetch for when v18 hits LTS. |
I don't quite understand whether node18 is lts or not, and whether fetch is experimental or not, what does it have to do with whether there is a definition of fetch in @types/node? Has anyone stipulated that all the apis defined in @types are stable? I do not think so. @types is just a type definition. Anything that can be called or accessed in js should have a corresponding type definition, regardless of whether it is experimental or not. |
I'm not so much concerned about being stable or not... Just only, i think if it's available without flags, It should be available for the same version. It's said, @types/node 18.0.0 should have all the types of Node.js 18, i can be happy of not having available a newer version or being It delayed until it's complete and on pair, and need to use and stick with @types/node 17.x.x, than install 18.x.x thinking It has all the same types and have some of them missing. At least, if It will provide only non-experimental ones, notify It in the readme. |
This wouldn't work because merely touching However, as a temporary workaround you could add a declaration file to your project that re-exports the types from import _fetch from 'node-fetch'
declare global {
declare var fetch: typeof _fetch;
}
|
I like the workaround, thanks |
I just noticed that this issue does not have a link to microsoft/TypeScript-DOM-lib-generator#1207 . As @HoldYourWaffle pointed out, the current lib-dom does not export types, it only makes global declarations. It would be great if those types were available for "clean" import, but it doesn't seem to be a priority yet. Maybe an upvote for that issue would help to solve this one? |
Upvoted and commented at microsoft/TypeScript-DOM-lib-generator#1207 (comment) |
What does experimental status, DOM modularity, or LTS have anything to do with whether or not types are included here? I don't see the point in arguing any of that - the point is, it exists in Node 18 without a flag. There should be types for it. No, sorry, DOM is not acceptable because Node does not use the DOM. WhatWG Fetch is a standard pulled out of the browsers and re-implemented in Node.js because it is useful. The fact it originated in browsers is completely irrelevant. Why is this a discussion? This is a hole in the types, plain and simple. |
The reason I brought up DOM modularity is basically #59905 . Lots of people wind up with the |
Also while I'm thinking of it, re @HoldYourWaffle 's comment about adding your own types for this, I wanted to suggest declare global {
var fetch: typeof import("node-fetch").default;
} I believe this makes it clear to the typechecker that you're 100% not trying to import Of course I do still think we need a path to correct types directly in |
Node.js uses EDIT: exposed directly (see #60924 (comment)) |
This probably isn't the place to discuss it, but my first thought on reading your opening paragraph was, has the TS team considered taking on a "lib.node.d.ts" so they can be in charge of ensuring conflicts don't happen? It seems precarious to put so much responsibility in the hands of a community maintained library. As you point out, it's probably a rare frontend project that doesn't wind up with |
@thw0rted if anything, the team is moving away from the bundled type declarations. https://www.npmjs.com/package/@types/web Though that is a somewhat different question than maintainence. |
@thw0rted I think both the collaboration between the TS team and the community on |
I think I have both a version that copies undici types and a version that has undici as a dependency working. The latter is satisfyingly short: declare namespace NodeJS {
namespace undici {
type Request = typeof globalThis extends { onmessage: any } ? {} : import("undici").Request;
type Response = typeof globalThis extends { onmessage: any } ? {} : import("undici").Response;
type File = typeof globalThis extends { onmessage: any } ? {} : import("undici").File;
type FormData = typeof globalThis extends { onmessage: any } ? {} : import("undici").FormData;
type Headers = typeof globalThis extends { onmessage: any } ? {} : import("undici").Headers;
}
}
declare function fetch (
input: import("undici").RequestInfo,
init?: import("undici").RequestInit
): Promise<Response>;
interface Request extends NodeJS.undici.Request {}
declare var Request: typeof globalThis extends {
onmessage: any;
Request: infer T;
} ? T : typeof import("undici").Request;
interface Response extends NodeJS.undici.Response {}
declare var Response: typeof globalThis extends {
onmessage: any;
Response: infer T;
} ? T : typeof import("undici").Response;
declare var File: typeof globalThis extends {
onmessage: any;
File: infer T;
} ? T : typeof import("undici").File;
interface File extends NodeJS.undici.File {}
declare var FormData: typeof globalThis extends {
onmessage: any;
FormData: infer T;
} ? T : typeof import("undici").FormData;
interface FormData extends NodeJS.undici.FormData {}
declare var Headers: typeof globalThis extends {
onmessage: any;
Headers: infer T;
} ? T : typeof import("undici").Headers;
interface Headers extends NodeJS.undici.Headers {}
Upon taking a closer look at how @thw0rted made the
Yeah. The signatures are identical with the other globals substituted, so adding the overload works perfectly fine. Planning to chat with the rest of the team tomorrow about which approach to PR. |
The feedback I got from the team was that we should try to avoid the undici-as-dependency approach mostly due to the significant impact it would have on |
Well DT is good for packages that don't have types already but for packages that do, or are otherwise written in TS, DT is usually the worse of the two options due to desynchronization. If there could be a way to extract types at build time, prior to publishing, that'd be the 'ideal' way depending on how you look at things. |
Prepublish script? |
It seems to me that this points to a fundamental problem with how TypeScript handles type definitions, particularly with global types. The actual TypeScript team should be more involved in this discussion. I think the best solution today is to use a clever combination of the An example web project might have 3 "environments" that are used by various files: a "web" environment for production code that will run in a browser, a "test" environment that runs in Node (or Deno or Bun) and polyfills web APIs using something like This project can handle the differences in available global APIs and modules by defining 3 separate tsconfig files with explicitly defined
This solves most of the problem, notably for files that run only on the web or only in Node. However, the "test" environment has both DOM and Node types. Technically we can't know which TypeScript's solution for this problem up to this point has been to merge both definitions into one. This works well for interfaces, which will simply accept properties from both definitions. It does not work well for functions, however. We already see this problem with As more and more web APIs are added to non-browser environments, this problem is likely to only get worse. For example, since We need the ability to more finely tune what an environment looks like, down to selecting individual items from various type definitions. This might be possible using conditional types and |
Oops! I didn't notice that someone from the TypeScript team was already here! I read the earlier comments more thoroughly than the latter ones, so I completely missed your responses. I'm sorry if I offended anyone. |
No worries 😄 I think you’ll find some of those links interesting. I think this issue is probably not the best place to go deep into the weeds on rethinking environmental types from the ground up. I broadly agree with you, and this problem used to be very much top-of-mind for me. But from my perspective, each time we’ve hit a wave of global type clashes that seems really intractable, we’ve actually been able to resolve things pretty well with the tools we already have. The PR I have up to add
Again, it’s not perfect, but it doesn’t provide a super strong justification for overhauling the way global types work. We just try our best, through DT contributions, to discourage Node.js-related typings from pulling in the DOM unnecessarily and vice versa, so users can hopefully avoid having I would love to have a more rigorous solution in the future, but it’s definitely not worth holding off on adding |
That makes a lot of sense. The Issues you posted are super interesting and show that others have clearly thought more about this than I have 😅 I agree that there needs to be a solution right now to resolve this particular issue. I'll defer further discussion to your PR. |
For now undici works. npm install undici import { fetch } from "undici"; |
#66824 is ready. It only updates v20 typings due to a test infrastructure limitation that will likely be resolved within a couple weeks. I’d like to wait for a non-TS team |
This is a giant thread and all i want is to know how to use the correct types for native fetch in nodejs. Does this example show the correct way to do types with node native fetch?
|
I believe fetch types are pretty good on the latest |
I use @types/node@18.0.0 version ,but use fetch fail,typescript to me "fetch is not defined"
Could I know , why "fetch is not defined" ?
The text was updated successfully, but these errors were encountered: