Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Types appear to be only additive, not subtractive when compared with JS Fetch API types #55

Closed
hansede opened this issue Jul 21, 2020 · 0 comments · Fixed by #112
Closed

Comments

@hansede
Copy link

hansede commented Jul 21, 2020

The Issue

Some of the JS Fetch API is not implemented in Cloudflare Workers. One example of this is Body.blob() on a Request object, which is implemented in the JS Fetch API, but calling this function in a Cloudflare Worker will result in an error:

Failed to execute 'blob' on 'Body': the method is not implemented.

The Cloudflare Workers reference appears to agree (by omission) that Body.blob is not implemented:
https://developers.cloudflare.com/workers/reference/apis/request/

However, when using the Request type provided by the @cloudflare/worker-types repo, using Body.blob does not cause a type error and instead appears to be implemented:
Screen Shot 2020-07-21 at 2 57 17 PM

Inspecting the types in this repo, it seems that the definition of Request is purely additive (adding the cf property) and does nothing to prohibit unimplemented properties:

interface Request {
  cf: IncomingRequestCfProperties;
}

A Possible Solution

Before discovering the official types repo for Cloudflare Workers, I implemented TypeScript types for my Workers from scratch. In order to make the types for Cloudflare's Fetch API omit properties from the JS Fetch API, I defined custom types prefixed with Cf, for example:

export default interface CfRequest {
  readonly clone: () => CfRequest;
  readonly cf: CfObject;
  readonly bodyUsed: boolean;
  readonly headers: Headers;
  readonly method: RequestMethod;
  readonly redirect: RequestRedirect;
  readonly text: () => Promise<string>;
  readonly url: string;
}

By generating a custom type that inherits nothing from the official Fetch API types, unimplemented properties like Body.blob can be omitted.

threepointone added a commit that referenced this issue Sep 30, 2021
Auto-Generated Types

Types are now automatically generated with releases by parsing the runtime's source code, and merging in overrides/docs defined in this repository (for generics, overloads, comments, etc).

webworker no longer needs to (and shouldn't) be included in tsconfig lib.

The final merged AST that's used to render the TypeScript types, workers.json, is also included in the repository. This could be used to generate bindings for other languages that compile to WebAssembly. Rust output is coming soon.

Closes: #55, #75, #76, #81, #84, #96, #97, #100, #101, #102, #105, #106, #107, #108
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant