`TextEncoder.encode` is typed `Uint8Array<ArrayBufferLike>` in workers-types; it returns a non-shared buffer

Open Beginner friendly
#8,807 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp, typescript
Domain
api

Research direction

The declaration to change is TextEncoder.encode, whose type is generated from the C++ bindings in src/workerd/api/encoding.h. Use the existing encodeInto JSG_TS_OVERRIDE in that file as the pattern, and check whether encodeUtf8Chunk-backed stream output or bytes() need the same change. Done when the repro in the issue compiles under strict mode with @cloudflare/workers-types, and the type tests pass.

Written by the indexing model from the issue text.

Description

In @cloudflare/workers-types 5.20261121.1, TextEncoder declares:

encode(input?: string): Uint8Array;

Since TypeScript 5.7 made typed arrays generic over their buffer, a bare Uint8Array means Uint8Array<ArrayBufferLike>, which includes SharedArrayBuffer. encode always returns a view over a fresh, non-shared ArrayBuffer, so the precise type is Uint8Array<ArrayBuffer>. TypeScript's own lib.dom.d.ts and lib.webworker.d.ts (5.9) declare encode(input?: string): Uint8Array<ArrayBuffer>, and @types/node declares NodeJS.NonSharedUint8Array.

Repro

TypeScript 7.0.2, @cloudflare/workers-types 5.20261121.1, "types": ["@cloudflare/workers-types"], "strict": true:

export function encode(value: string): Uint8Array<ArrayBuffer> {
  return new TextEncoder().encode(value);
}
error TS2322: Type 'Uint8Array<ArrayBufferLike>' is not assignable to type 'Uint8Array<ArrayBuffer>'.
  Type 'ArrayBufferLike' is not assignable to type 'ArrayBuffer'.
    Type 'SharedArrayBuffer' is not assignable to type 'ArrayBuffer'.

Why it matters

Code that runs on both Node and Workers has to type the result as Uint8Array<ArrayBuffer>, because Node's and lib.dom's BufferSource reject ArrayBufferLike-backed views. With workers-types loaded, the same code then needs a cast or a Uint8Array.from(...) copy.

Request

Declare encode(input?: string): Uint8Array<ArrayBuffer> (for example with a JSG_TS_OVERRIDE in src/workerd/api/encoding.h, as encodeInto already has). Other APIs that return freshly allocated buffers, such as encodeUtf8Chunk-backed streams or bytes(), may have the same widening.

Related: #7833 tracks rejecting SharedArrayBuffer-backed views at runtime in SubtleCrypto. That issue is about runtime behaviour; this one is only about the declared return type.

Dominant language
C++
Stars
9.3k
Forks
845
Avg merge
1d 23h
Merged PRs (30d)
558

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cloudflare/workerd

All issues in cloudflare/workerd

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.