Skip to content

Incorrect type returned by TextEncoder.encodeΒ #60846

Open
@aryzing

Description

@aryzing

πŸ”Ž 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions