Typescript: Some methods are missing from Blob interface

Created on 22 Oct 2019  Β·  6Comments  Β·  Source: microsoft/TypeScript

The arrayBuffer(), stream(), and text() of Blob are missing from the type definition. (See https://w3c.github.io/FileAPI/#stream-method-algo and following for details.)


TypeScript Version: 3.7.0-dev.20191021


Search Terms: blob text method typescript

Code

declare var x: Blob;

x = new Blob();
x.slice();
x.text();

Expected behavior: Program typechecks.

Actual behavior: error TS2339: Property 'text' does not exist on type 'Blob'.

Playground Link: https://www.typescriptlang.org/play/?ts=3.7.0-dev.20191021#code/CYUwxgNghgTiAEA3W8AeAueAhCB7ARgNwCwAUGavALzwB2IA7tnvgBQCUJpqAdAM4QAlmBAcuvAC4hUEsUA

Related Issues: none

Most helpful comment

You could also call it like x['text'](), if you don't want to redeclare the interface.

All 6 comments

You're right.

What's the best way for me to "monkeypatch" these into my code if I really want to make use of them? I imagine I can't just redeclare interface Blob and expect everything to just work?

You should be able to use declaration merging for this and provide your own declarations:

https://www.typescriptlang.org/docs/handbook/declaration-merging.html

Thank you for the pointer!

Ran into this same problem in my create-react-app

Added this to src/react-app-env.d.ts

interface Blob {
  text: () => Promise<string>
}

You could also call it like x['text'](), if you don't want to redeclare the interface.

Was this page helpful?
0 / 5 - 0 ratings