Microsoft/TypeScript

Incorrect type returned by TextEncoder.encode

Open

#60.846 geöffnet am 23. Dez. 2024

Auf GitHub ansehen
 (3 Kommentare) (3 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: lib.d.tsHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

🔎 Search Terms

TypedArray, Uint8Array, ArrayBufferLike, TextEncoder

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about TextEncoder

⏯ Playground Link

Playground link

💻 Code

const data = new TextEncoder().encode("hello")
//    ^? Uint8Array<ArrayBufferLike>
// The `ArrayBufferLike` may include `SharedArrayBuffer`, but `encode()` 
// only supports `ArrayBuffer`, so this breaks code expecting an `ArrayBuffer`.

function processData(d: ArrayBuffer) {
  // do stuff
}

processData(data.buffer) // This should work, but reported as type error

🙁 Actual behavior

Values returned by TextEncoder.encode are not assignable to ArrayBuffer.

🙂 Expected behavior

Values returned by TextEncoder.encode should be assignable to ArrayBuffer.

Additional information about the issue

https://stackoverflow.com/a/79300250/1494725

Contributor Guide