microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.21k stars 12.51k forks source link

Type 'ArrayBufferLike' is not assignable to type 'BlobPart'. #60579

Open Yonom opened 5 hours ago

Yonom commented 5 hours ago

πŸ”Ž Search Terms

ArrayBufferLike BlobPart SharedArrayBuffer ES2024

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?target=11&ts=5.7.2#code/MYewdgzgLgBFICEA2IBGMC8MAUATAhlPgFwwCqAlmFABwCCATg-gJ4A0cLADgKanQMqAcwCUmAHwwA3jABQMGAx5QArgzAwwPAO4xkabAG0CRAHSoVAM0s8GAXQ4yo3HjAC+IgNyy3QA

πŸ’» Code

When targetting ES2024, the following code throws a type error:

const toBlob = (data: Uint8Array, type: string) => { 
  return new Blob([data.buffer], { type });
}

πŸ™ Actual behavior

Type 'ArrayBufferLike' is not assignable to type 'BlobPart'.
  Type 'SharedArrayBuffer' is not assignable to type 'BlobPart'.
    Type 'SharedArrayBuffer' is missing the following properties from type 'ArrayBuffer': resizable, resize, detached, transfer, transferToFixedLength

πŸ™‚ Expected behavior

Backwards compatibility with ES2023

Additional information about the issue

No response

jcalz commented 2 hours ago

The TS 5.7 release notes and #59417 explain what's going on here. If you want a Uint8Array whose buffer property is a suitable BlobPart you'll have to ask for it explicitly like

const toBlob = (data: Uint8Array<ArrayBuffer>, type: string) => { 
  return new Blob([data.buffer], { type });
}

Playground link

And of course that might have to propagate throughout your code base. Maybe the TS team has a recommendation for how to proceed in general with this sort of thing; the release notes only mention updating node.