Skip to content
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

Do not redeclare TS lib #1384

Closed
james-pre opened this issue Nov 3, 2023 · 1 comment
Closed

Do not redeclare TS lib #1384

james-pre opened this issue Nov 3, 2023 · 1 comment
Assignees
Labels
types Related to @cloudflare/workers-types

Comments

@james-pre
Copy link

After diving into worker-types with #1383, I realized that many of the workers-types are just modified from TS lib. Is there a reason for this? Why not extend pre-existing classes where needed?

For example, instead of re-declaring Body:

worker-types currently:

export declare abstract class Body {
  readonly body: ReadableStream | null;
  readonly bodyUsed: boolean;
  arrayBuffer(): Promise<ArrayBuffer>;
  text(): Promise<string>;
  json<T>(): Promise<T>;
  formData(): Promise<FormData>;
  blob(): Promise<Blob>;
}

lib.dom.d.ts (comments removed):

interface Body {
    readonly body: ReadableStream<Uint8Array> | null;
    readonly bodyUsed: boolean;
    arrayBuffer(): Promise<ArrayBuffer>;
    blob(): Promise<Blob>;
    formData(): Promise<FormData>;
    json(): Promise<any>;
    text(): Promise<string>;
}

Updated:
worker-types/alias.d.ts

export { Body };

worker-types/index.d.ts

import type { Body as FetchBody } from './alias';

export declare abstract class Body extends FetchBody {
  json<T>(): Promise<T>;
}

This would add a ton of reusability and compatibility to the types since the only code is changes from built-in classes. The following classes can use this approach:

  • DOMException
  • WorkerGlobalScope
  • WorkerGlobalScopeEventMap
  • Console
  • console
  • BufferSource
  • WebAssembly
  • ServiceWorkerGlobalScope
  • addEventListener
  • removeEventListener
  • dispatchEvent
  • btoa
  • atob
  • setTimeout
  • clearTimeout
  • setInterval
  • queueMicrotask
  • structuredClone
  • fetch
  • ...

I won't bother to list the 250+ symbols which only redeclare ones already present in TS lib, with little to no change.

@james-pre james-pre added the types Related to @cloudflare/workers-types label Nov 3, 2023
@penalosa
Copy link
Collaborator

penalosa commented Nov 6, 2023

This is intentional. The types for @cloudflare/workers-types are generated directly from the workers runtime implementation, and so accurately match runtime behaviour. In most cases we're pretty close to spec-compliant with web standards, and so are similar to lib.dom.d.ts, but we recommend using @cloudflare/workers-types exclusively, and not typing your codebase against lib.dom.d.ts.

@penalosa penalosa closed this as completed Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Related to @cloudflare/workers-types
Projects
None yet
Development

No branches or pull requests

3 participants