cloudflare / cloudflare/workerd

Lack of extends means you can't pass a clone of a Request to a fetch/new Request

Open
#4,402 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
8.7k
Forks
739
Avg merge
2d 20h
Merged PRs (30d)
174

Description

https://github.com/cloudflare/workerd/blob/2b08a7caa4681f5c0e9cde1dd4da9c857e3ca42f/types/generated-snapshot/latest/index.d.ts#L1656

This should become `interface Request>` or possibly even just `interface Request>`.

Without this change typescript complains about doing
```typescript
export default {
async fetch(request: Request, env: WorkerEnv): Promise {
return await fetch('http://someOtherURL.com', request.clone()); // <-- here
}
}
```
and
```
export default {
async fetch(request: Request, env: WorkerEnv): Promise {
return await fetch(request.clone()); // <-- here
}
}
```
and
```
export default {
async fetch(request: Request, env: WorkerEnv): Promise {
const loggingRequest = new Request(request.clone()); // <-- here

...
}
}
```

with the same error:
```
No overload matches this call.
The last overload gave the following error.
Argument of type 'Request' is not assignable to parameter of type 'RequestInit>'.
Types of property 'cf' are incompatible.
Type 'Cf | undefined' is not assignable to type 'CfProperties | undefined'.
Type 'Cf' is not assignable to type 'CfProperties | undefined'.
Type 'Cf' is not assignable to type 'IncomingRequestCfProperties'.
Type 'Cf' is not assignable to type 'IncomingRequestCfPropertiesBase'.ts(2769)
worker-configuration.d.ts(1340, 45): This type parameter might need an `extends IncomingRequestCfPropertiesBase` constraint.
worker-configuration.d.ts(1340, 45): This type parameter might need an `extends IncomingRequestCfProperties` constraint.
worker-configuration.d.ts(1340, 45): This type parameter might need an `extends CfProperties | undefined` constraint.
lib.dom.d.ts(29329, 18): The last overload is declared here.
```

Passing the request itself doesn't annoy the type-checker, but empirically that means the `Request` won't get copied/cloned correctly, so using the same request more than once in this manner will result in an error from using a request with a used body for every call except the first. Wrapping the request in a `new Request()` call doesn't annoy the type checker but that doesn't handle copying the body safely (i'm not going to pretend I understand the MDN docs enough to say for certain, but [this part](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#creating_a_request_object) of the MDN docs makes me think that might be a bug somewhere). However questionable the first 2 examples are, though, `new Request(request.clone())` should work because the spec explicitly says you can pass a `Request` object as the first argument to the `Request` constructor.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.