microsoft / microsoft/TypeScript
Incorrect type returned by TextEncoder.encode
Open
Nobody has claimed this yet.
Bug
Domain: lib.d.ts
Help Wanted
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
🔎 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
💻 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
Contributor 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.
Research direction
Start with the linked TypeScript Playground example and inspect the declarations and type-checking behavior for TextEncoder.encode and its buffer. The work is done when the returned buffer is accepted by processData as an ArrayBuffer without incorrectly allowing SharedArrayBuffer.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100