node.js - 如何将 Blob 添加到 Typescript?

标签 node.js typescript blob

我正在尝试使用 FileSaver 保存文件,这必然需要 Blob 格式。我的应用程序是用 typescript 编写的。

当我尝试做的时候

import * as Blob from "blob";

我收到错误

Could not find a declaration file for module 'blob'. '/Users/path/to/project/node_modules/blob/index.js' implicitly has an 'any' type.

Try \npm install @types/blob` if it exists or add a new declaration (.d.ts) file containing `declare module 'blob';``

我不能只导入“blob”,因为 @types/blob 包不存在。

我用这些内容创建了一个声明文件 blob.d.ts

declare module 'blob' {
  function Blob(blobParts[, options]): any
  export = Blob
}

基于MDN entry on the Blob constructor

这并没有解决我需要 Blob 的一行代码的问题

var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});

它返回下面的错误

Argument of type '{ type: string; }' is not assignable to parameter of type '[any, any]'. Object literal may only specify known properties, and 'type' does not exist in type '[any, any]'.

similar question不是重复的,它的答案对我没有帮助,因为该声明文件没有声明模块,也没有导出任何东西。

最佳答案

在你的tsconfig.json , 请确保包含 webworker在你的lib数组:

{
  "compilerOptions": {
    ...
    "lib": ["webworker"],
    ...
  },
  ...
}

尽管从上面提供的引用链接中应该已经很明显,lib.webworker.d.ts 包含 Blob 和相关接口(interface)所需的声明。

interface Blob {
    readonly size: number;
    readonly type: string;
    slice(start?: number, end?: number, contentType?: string): Blob;
}

declare var Blob: {
    prototype: Blob;
    new(blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
};

关于node.js - 如何将 Blob 添加到 Typescript?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56862973/

相关文章:

node.js - 返回或不回调,对 Node 有影响吗

javascript - 环回: Making synchronous call to loopback api

javascript - contenteditable 到 javascript 中的 azure 存储中的文本文件

php - 我尝试使用 Android 应用程序的 php Web 服务从 mysql 数据库检索 blob(图像),但 blob 列仍然为空

node.js - 无法使用 Mongoose 连接到 mongodb 图集

javascript - React Router/Express - 刷新动态 URL 参数

angular - ionic 3 单击按钮后从 ion-textarea 获取文本

typescript - 如何使用联合类型修复 typescript "Property does not exist on type"?

javascript - Typescript 拒绝不符合接口(interface)的属性

mysql - 如何在mysql中插入blob数据