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

Types: TextEncoder.encode() has incorrect type #1149

Closed
bachmacintosh opened this issue Sep 8, 2023 · 0 comments · Fixed by #1158
Closed

Types: TextEncoder.encode() has incorrect type #1149

bachmacintosh opened this issue Sep 8, 2023 · 0 comments · Fixed by #1158
Assignees
Labels
types Related to @cloudflare/workers-types

Comments

@bachmacintosh
Copy link

After attempting to update @cloudflare/workers-types to v4.20230904.0, use of the TextEncoder.encode() function is throwing an error in TypeScript as the return type provided by CloudFlare's types differs from the web standard. This doesn't impact actual deployments to CloudFlare, so likely just an issue with the generated types. Both the main entrypoint and multiple compatibility date entrypoints are affected.

Reproduction

Minimal reproduction: https://github.com/bachmacintosh/cf-worker-text-encoder-issue

  1. Create a new Worker project (e.g. using npm create cloudflare@latest and using TypeScript)
  2. Create an encoded string using TextEncoder.encode()
  3. Try accessing the BYTES_PER_ELEMENT constant on the returned result, such as with a console.log()

Expected Behavior

The BYTES_PER_ELEMENT constant should be accessible and have a value of 1, since the returned object should be a UInt8Array per the web standard. TypeScript should not throw any error.

Actual Behavior

The returned type is inferred by TypeScript as ArrayBuffer | ArrayBufferView which does not have a BYTES_PER_ELEMENT property -- throwing this error in TypeScript:

Property 'BYTES_PER_ELEMENT' does not exist on type 'ArrayBuffer | ArrayBufferView'.
  Property 'BYTES_PER_ELEMENT' does not exist on type 'ArrayBuffer'.

However when actually accessing the value on the deployed worker, the expected value -- 1 -- is logged.

Let me know if any more information is required. Thank you!

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

Successfully merging a pull request may close this issue.

2 participants