`TextEncoder.encode` is typed `Uint8Array<ArrayBufferLike>` in workers-types; it returns a non-shared buffer
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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudflare/workerd
-
Build fails on macOS 27: dyld refuses stripped Rust proc-macros (mis-aligned LINKEDIT string pool)Open
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
cloudflare/workerd#8963 ·
Maintainers usually reply within 1 day
-
D1 export: `quote()` in d1.js turns a literal `\n` into a newline when the value also has a real newlinePossibly taken @Jeremiah-D claimed this 8 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
cloudflare/workerd#8678 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudflare/workerd#8575 ·
Maintainers usually reply within 1 day
-
fix(types): ForwardableEmailMessage omits the documented canBeForwarded propertyPossibly taken @itsrayeedscodespace claimed this 19 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cloudflare/workerd#8290 ·
Maintainers usually reply within 1 day
-
Streaming TextDecoder drops U+FEFF at the start of a chunk that follows an ASCII-only chunkMay be free again @oliarmak-web claimed this 19 days ago, and no pull request is open. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
cloudflare/workerd#8279 · 1 comment ·
Maintainers usually reply within 1 day
All issues in cloudflare/workerd
Similar issues
-
Android Bug Core Drape
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
organicmaps/organicmaps#14449 ·
Maintainers usually reply within 1 day
-
comp-codecs
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
ClickHouse/ClickHouse#135159 ·
Maintainers usually reply within 1 day
-
difficulty:easy func:python os:windows prio:high type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
NatronGitHub/Natron#2029 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
ardazishvili/Sota#114 ·
Maintainers usually reply within 1 day
-
`Rect::containsPoint` is not `const`, so a `const Rect` cannot be asked whether it holds a pointOpenNeeds: Triage :mag:
Difficulty 1/5 Under an hour Newbie friendliness 82/100
react/react-native#60349 ·
Maintainers usually reply within 1 day