oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.3k stars 2.78k forks source link

TypeScript: 'proxy' property not recognized in fetch RequestInit in Bun.js #13827

Open JoShMiQueL opened 2 months ago

JoShMiQueL commented 2 months ago

What version of Bun is running?

1.1.27+267afa293

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

  1. Create a new project using Bun.js and TypeScript.
  2. Write an asynchronous function using fetch to make a request, including a proxy property in the RequestInit object.
  3. Open the project in Visual Studio Code with TypeScript support enabled (e.g., using the TypeScript extension).
  4. Add the following code in a TypeScript file:
    
    const request = async () => {
    const response = await fetch(
    "https://example-web.com/",
    {
      method: "GET",
      proxy: "http://your-proxy-url:port", // <-- VSCode display an error here
    }
    );
    return response;
    };

request().then(console.log).catch(console.error);

5. Notice that **VSCode** displays a TypeScript error saying the `proxy` property does not exist in the `RequestInit` type.
6. Despite the error in VSCode, the code still runs without issues when compiled and executed with **Bun.js**.

### What is the expected behavior?

The TypeScript type definitions in **VSCode** should recognize the `proxy` property as valid within the `RequestInit` type when using `fetch` in **Bun.js**. No TypeScript error or warning should appear in the editor, and the property should be fully supported with proper type hinting and autocomplete in the IDE.

### What do you see instead?

In **Visual Studio Code**, a TypeScript error is displayed in the editor, stating that the `proxy` property does not exist in the `RequestInit` type. The error message is:
```typescript
No overload matches this call.
Overload 1 of 2, '(input: RequestInfo, init?: RequestInit | undefined): Promise<Response>', gave the following error.
Object literal may only specify known properties, and 'proxy' does not exist in type 'RequestInit'.

Despite this, the code runs without any issues when compiled and executed with Bun.js.

Additional information

No response

ragavpr commented 2 months ago

This workaround works, atleast for me

const response = await fetch("https://example-web.com/", {
  method: "GET",
  proxy: "http://your-proxy-url:port",
} as FetchRequestInit); // <-- Workaround

probably related in these two .d.ts files https://github.com/oven-sh/bun/blob/3d68a9483fbb69b7ac4dd9c686658ec14a6674e4/packages/bun-types/globals.d.ts#L929-L931 https://github.com/oven-sh/bun/blob/3d68a9483fbb69b7ac4dd9c686658ec14a6674e4/packages/bun-types/bun.d.ts#L3173-L3175

JoShMiQueL commented 2 months ago

Thanks for the workaround! It works great. I also came up with a temporary solution to fix the editor issue by creating a .d.ts file. Here's what I added:

// bun-fetch.d.ts
interface RequestInit {
  proxy?: string;
  verbose?: boolean;
}

This resolves the type error in the code editor for now.

ragavpr commented 2 months ago

There is another possibility of VSCode inferring wrong type due to TypeScript extension. /opt/vscodium-bin/resources/app/extensions/node_modules/typescript/lib/lib.dom.d.ts

declare function fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;

There is no method to influence it from bun, it can be avoided by clearly using only Bun's fetch.

Either with imports

import { fetch } from 'bun';

or with Bun prefix

const res = await Bun.fetch(...);