Types: TextEncoder.encode()
has incorrect type
#1149
Labels
types
Related to @cloudflare/workers-types
TextEncoder.encode()
has incorrect type
#1149
After attempting to update
@cloudflare/workers-types
tov4.20230904.0
, use of theTextEncoder.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
npm create cloudflare@latest
and using TypeScript)TextEncoder.encode()
BYTES_PER_ELEMENT
constant on the returned result, such as with aconsole.log()
Expected Behavior
The
BYTES_PER_ELEMENT
constant should be accessible and have a value of 1, since the returned object should be aUInt8Array
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 aBYTES_PER_ELEMENT
property -- throwing this error in TypeScript: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!
The text was updated successfully, but these errors were encountered: