Skip to content

Conversation

@magurotuna
Copy link
Member

This patch updates the TextEncoder interface type declarations to correctly specify ArrayBuffer as the backing buffer type for the encode() method, aligning with the spec and addressing the change introduced in TypeScript 5.7 about making ArrayVBufferLike a default buffer type behind Uint8Array.

Problem

TypeScript 5.7 changed the default backing buffer type for Uint8Array from ArrayBuffer to ArrayBufferLike (which is a union of ArrayBuffer and SharedArrayBuffer). However, TextEncoder.encode() per specification always returns a Uint8Array backed by a standard ArrayBuffer, never a SharedArrayBuffer. This mismatch causes type errors when passing the buffer to functions expecting ArrayBuffer:

const data = new TextEncoder().encode("hello");

function processData(d: ArrayBuffer) { /* ... */ }

processData(data.buffer);

Type check result with Deno v2.4.4:

Check file:///tmp/arraybuffer.ts
TS2345 [ERROR]: Argument of type 'ArrayBufferLike' is not assignable to parameter of type 'ArrayBuffer'.
  Type 'SharedArrayBuffer' is missing the following properties from type 'ArrayBuffer': resizable, resize, detached, transfer, transferToFixedLength
processData(data.buffer) // Type error: ArrayBufferLike is not assignable to ArrayBuffer
            ~~~~~~~~~~~
    at file:///tmp/arraybuffer.ts:7:13

error: Type checking failed.

Solution

This issue is resolved in this patch by explicitly specifying Uint8Array<ArrayBuffer> as the return type for encode().

Also, the second parameter type of encodeInto() is updated to Uint8Array<ArrayBufferLike> to make it less confusing and more explicit.

Refs

Comment on lines -455 to -460
/** @category Encoding */
interface TextEncoder extends TextEncoderCommon {
/** Returns the result of running UTF-8's encoder. */
encode(input?: string): Uint8Array;
encodeInto(input: string, dest: Uint8Array): TextEncoderEncodeIntoResult;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this interface because it simply appears to be a duplicate with less document

@dsherret
Copy link
Member

dsherret commented Aug 18, 2025

I think this specific issue was fixed in TypeScript 5.9. We should wait for that before landing probably and wait for the minor release because this is breaking.

https://devblogs.microsoft.com/typescript/announcing-typescript-5-9/#lib.d.ts-changes

@petamoriken
Copy link
Contributor

Since TypeScript 4.7, ArrayBuffer and SharedArrayBuffer have been distinguished when using types from ES2024. As of TypeScript 4.9, this distinction is also made for types from ES2023 and earlier (by microsoft/TypeScript#60150).

Personally, I don't feel the need to wait for TypeScript 4.9, but I don't have a strong opinion.

@dsherret
Copy link
Member

This specific issue was only fixed recently in microsoft/TypeScript-DOM-lib-generator#1944 for TS 5.9, so I think we should wait in order to align (plus it's a breaking change)

@petamoriken
Copy link
Contributor

That's true. That makes sense 👍

@magurotuna
Copy link
Member Author

oh ok thanks for the detail. let's wait for that

@magurotuna magurotuna closed this Aug 19, 2025
@magurotuna magurotuna reopened this Aug 19, 2025
@bartlomieju bartlomieju added this to the 2.5.0 milestone Sep 2, 2025
Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now

@dsherret dsherret merged commit 6523e3f into denoland:main Sep 2, 2025
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants